Note that it does not work to tell Gson to deserialize the first example as type Object. Object data = gson.fromJson("[3]", Object.class); results in a parse exception complaining that [3] is not a primitive.
What confuses me however, is how to delegate back into json serialization... Most of the time I need this for collections, but to illustrate the point - suppose I had a pair class, which gson would obviously serialize happily, but for some reason I needed my own custom serializer. Well... if my pair is
So you can see the problem - I have no idea the type of first and second, nor how they are serialized. I can use gson.toJson to serialize first and second - but if I add them as a string to the writer, they will be escaped. There is a gson.tojson function that takes a value and a writer - but it also takes a typetoken - which I don't have. I sort of get the impression I'm meant to have another type adapter from somewhere - but when I just have a list of objects... where do I get that? do I just get the adapter for object?
Gson uses Java reflection API to get the type of the object to which a Json text is to be mapped. But with generics, this information is lost during serialization. To counter this problem, Gson provides a class com.google.gson.reflect.TypeToken to store the type of the generic object.
You could try a script that looks at each view.json on the filesystem; it looks like the one that's an issue has no line breaks and is totally invalid (a bad script to write an updated view json, maybe?)
Based on: Caused by: com.inductiveautomation.ignition.common.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 6149 path $
Let's say we want to write a type adapter that counts the number of objects being read from or written to JSON. We can achieve this by writing a type adapter factory that uses the getDelegateAdapter method: class StatsTypeAdapterFactory implements TypeAdapterFactory public int numReads = 0; public int numWrites = 0; public TypeAdapter create(Gson gson, TypeToken type) final TypeAdapter delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter() public void write(JsonWriter out, T value) throws IOException ++numWrites; delegate.write(out, value); public T read(JsonReader in) throws IOException ++numReads; return delegate.read(in); ; This factory can now be used like this: StatsTypeAdapterFactory stats = new StatsTypeAdapterFactory(); Gson gson = new GsonBuilder().registerTypeAdapterFactory(stats).create(); // Call gson.toJson() and fromJson methods on objects System.out.println("Num JSON reads" + stats.numReads); System.out.println("Num JSON writes" + stats.numWrites); Note that this call will skip all factories registered before skipPast. In case of multiple TypeAdapterFactories registered it is up to the caller of this function to insure that the order of registration does not prevent this method from reaching a factory they would expect to reply from this call. Note that since you can not override type adapter factories for String and Java primitive types, our stats factory will not count the number of String or primitives that will be read or written.Parameters:skipPast - The type adapter factory that needs to be skipped while searching for a matching type adapter. In most cases, you should just pass this (the type adapter factory from where getDelegateAdapter(com.google.gson.TypeAdapterFactory, com.google.gson.reflect.TypeToken) method is being invoked).type - Type for which the delegate adapter is being searched for.Since:2.2getAdapterpublic TypeAdapter getAdapter(java.lang.Class type)Returns the type adapter for type.Throws:java.lang.IllegalArgumentException - if this GSON cannot serialize and deserialize type.toJsonTreepublic JsonElement toJsonTree(java.lang.Object src)This method serializes the specified object into its equivalent representation as a tree of JsonElements. This method should be used when the specified object is not a generic type. This method uses Object.getClass() to get the type for the specified object, but the getClass() loses the generic type information because of the Type Erasure feature of Java. Note that this method works fine if the any of the object fields are of generic type, just the object itself should not be of a generic type. If the object is of generic type, use toJsonTree(Object, Type) instead.Parameters:src - the object for which Json representation is to be created setting for GsonReturns:Json representation of src.Since:1.4toJsonTreepublic JsonElement toJsonTree(java.lang.Object src, java.lang.reflect.Type typeOfSrc)This method serializes the specified object, including those of generic types, into its equivalent representation as a tree of JsonElements. This method must be used if the specified object is a generic type. For non-generic objects, use toJsonTree(Object) instead.Parameters:src - the object for which JSON representation is to be createdtypeOfSrc - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection, you should use: Type typeOfSrc = new TypeToken().getType(); Returns:Json representation of srcSince:1.4toJsonpublic java.lang.String toJson(java.lang.Object src)This method serializes the specified object into its equivalent Json representation. This method should be used when the specified object is not a generic type. This method uses Object.getClass() to get the type for the specified object, but the getClass() loses the generic type information because of the Type Erasure feature of Java. Note that this method works fine if the any of the object fields are of generic type, just the object itself should not be of a generic type. If the object is of generic type, use toJson(Object, Type) instead. If you want to write out the object to a Writer, use toJson(Object, Appendable) instead.Parameters:src - the object for which Json representation is to be created setting for GsonReturns:Json representation of src.toJsonpublic java.lang.String toJson(java.lang.Object src, java.lang.reflect.Type typeOfSrc)This method serializes the specified object, including those of generic types, into its equivalent Json representation. This method must be used if the specified object is a generic type. For non-generic objects, use toJson(Object) instead. If you want to write out the object to a Appendable, use toJson(Object, Type, Appendable) instead.Parameters:src - the object for which JSON representation is to be createdtypeOfSrc - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection, you should use: Type typeOfSrc = new TypeToken().getType(); Returns:Json representation of srctoJsonpublic void toJson(java.lang.Object src, java.lang.Appendable writer) throws JsonIOExceptionThis method serializes the specified object into its equivalent Json representation. This method should be used when the specified object is not a generic type. This method uses Object.getClass() to get the type for the specified object, but the getClass() loses the generic type information because of the Type Erasure feature of Java. Note that this method works fine if the any of the object fields are of generic type, just the object itself should not be of a generic type. If the object is of generic type, use toJson(Object, Type, Appendable) instead.Parameters:src - the object for which Json representation is to be created setting for Gsonwriter - Writer to which the Json representation needs to be writtenThrows:JsonIOException - if there was a problem writing to the writerSince:1.2toJsonpublic void toJson(java.lang.Object src, java.lang.reflect.Type typeOfSrc, java.lang.Appendable writer) throws JsonIOExceptionThis method serializes the specified object, including those of generic types, into its equivalent Json representation. This method must be used if the specified object is a generic type. For non-generic objects, use toJson(Object, Appendable) instead.Parameters:src - the object for which JSON representation is to be createdtypeOfSrc - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection, you should use: Type typeOfSrc = new TypeToken().getType(); writer - Writer to which the Json representation of src needs to be written.Throws:JsonIOException - if there was a problem writing to the writerSince:1.2toJsonpublic void toJson(java.lang.Object src, java.lang.reflect.Type typeOfSrc, JsonWriter writer) throws JsonIOExceptionWrites the JSON representation of src of type typeOfSrc to writer.Throws:JsonIOException - if there was a problem writing to the writertoJsonpublic java.lang.String toJson(JsonElement jsonElement)Converts a tree of JsonElements into its equivalent JSON representation.Parameters:jsonElement - root of a tree of JsonElementsReturns:JSON String representation of the treeSince:1.4toJsonpublic void toJson(JsonElement jsonElement, java.lang.Appendable writer) throws JsonIOExceptionWrites out the equivalent JSON for a tree of JsonElements.Parameters:jsonElement - root of a tree of JsonElementswriter - Writer to which the Json representation needs to be writtenThrows:JsonIOException - if there was a problem writing to the writerSince:1.4newJsonWriterpublic JsonWriter newJsonWriter(java.io.Writer writer) throws java.io.IOExceptionReturns a new JSON writer configured for the settings on this Gson instance.Throws:java.io.IOExceptionnewJsonReaderpublic JsonReader newJsonReader(java.io.Reader reader)Returns a new JSON reader configured for the settings on this Gson instance.toJsonpublic void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOExceptionWrites the JSON for jsonElement to writer.Throws:JsonIOException - if there was a problem writing to the writerfromJsonpublic T fromJson(java.lang.String json, java.lang.Class classOfT) throws JsonSyntaxExceptionThis method deserializes the specified Json into an object of the specified class. It is not suitable to use if the specified class is a generic type since it will not have the generic type information because of the Type Erasure feature of Java. Therefore, this method should not be used if the desired type is a generic type. Note that this method works fine if the any of the fields of the specified object are generics, just the object itself should not be a generic type. For the cases when the object is of generic type, invoke fromJson(String, Type). If you have the Json in a Reader instead of a String, use fromJson(Reader, Class) instead.Type Parameters:T - the type of the desired objectParameters:json - the string from which the object is to be deserializedclassOfT - the class of TReturns:an object of type T from the string. Returns null if json is null or if json is empty.Throws:JsonSyntaxException - if json is not a valid representation for an object of type classOfTfromJsonpublic T fromJson(java.lang.String json, java.lang.reflect.Type typeOfT) throws JsonSyntaxExceptionThis method deserializes the specified Json into an object of the specified type. This method is useful if the specified object is a generic type. For non-generic objects, use fromJson(String, Class) instead. If you have the Json in a Reader instead of a String, use fromJson(Reader, Type) instead.Type Parameters:T - the type of the desired objectParameters:json - the string from which the object is to be deserializedtypeOfT - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection, you should use: Type typeOfT = new TypeToken().getType(); Returns:an object of type T from the string. Returns null if json is null.Throws:JsonParseException - if json is not a valid representation for an object of type typeOfTJsonSyntaxException - if json is not a valid representation for an object of typefromJsonpublic T fromJson(java.io.Reader json, java.lang.Class classOfT) throws JsonSyntaxException, JsonIOExceptionThis method deserializes the Json read from the specified reader into an object of the specified class. It is not suitable to use if the specified class is a generic type since it will not have the generic type information because of the Type Erasure feature of Java. Therefore, this method should not be used if the desired type is a generic type. Note that this method works fine if the any of the fields of the specified object are generics, just the object itself should not be a generic type. For the cases when the object is of generic type, invoke fromJson(Reader, Type). If you have the Json in a String form instead of a Reader, use fromJson(String, Class) instead.Type Parameters:T - the type of the desired objectParameters:json - the reader producing the Json from which the object is to be deserialized.classOfT - the class of TReturns:an object of type T from the string. Returns null if json is at EOF.Throws:JsonIOException - if there was a problem reading from the ReaderJsonSyntaxException - if json is not a valid representation for an object of typeSince:1.2fromJsonpublic T fromJson(java.io.Reader json, java.lang.reflect.Type typeOfT) throws JsonIOException, JsonSyntaxExceptionThis method deserializes the Json read from the specified reader into an object of the specified type. This method is useful if the specified object is a generic type. For non-generic objects, use fromJson(Reader, Class) instead. If you have the Json in a String form instead of a Reader, use fromJson(String, Type) instead.Type Parameters:T - the type of the desired objectParameters:json - the reader producing Json from which the object is to be deserializedtypeOfT - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection, you should use: Type typeOfT = new TypeToken().getType(); Returns:an object of type T from the json. Returns null if json is at EOF.Throws:JsonIOException - if there was a problem reading from the ReaderJsonSyntaxException - if json is not a valid representation for an object of typeSince:1.2fromJsonpublic T fromJson(JsonReader reader, java.lang.reflect.Type typeOfT) throws JsonIOException, JsonSyntaxExceptionReads the next JSON value from reader and convert it to an object of type typeOfT. Returns null, if the reader is at EOF. Since Type is not parameterized by T, this method is type unsafe and should be used carefullyThrows:JsonIOException - if there was a problem writing to the ReaderJsonSyntaxException - if json is not a valid representation for an object of typefromJsonpublic T fromJson(JsonElement json, java.lang.Class classOfT) throws JsonSyntaxExceptionThis method deserializes the Json read from the specified parse tree into an object of the specified type. It is not suitable to use if the specified class is a generic type since it will not have the generic type information because of the Type Erasure feature of Java. Therefore, this method should not be used if the desired type is a generic type. Note that this method works fine if the any of the fields of the specified object are generics, just the object itself should not be a generic type. For the cases when the object is of generic type, invoke fromJson(JsonElement, Type).Type Parameters:T - the type of the desired objectParameters:json - the root of the parse tree of JsonElements from which the object is to be deserializedclassOfT - The class of TReturns:an object of type T from the json. Returns null if json is null.Throws:JsonSyntaxException - if json is not a valid representation for an object of type typeOfTSince:1.3fromJsonpublic T fromJson(JsonElement json, java.lang.reflect.Type typeOfT) throws JsonSyntaxExceptionThis method deserializes the Json read from the specified parse tree into an object of the specified type. This method is useful if the specified object is a generic type. For non-generic objects, use fromJson(JsonElement, Class) instead.Type Parameters:T - the type of the desired objectParameters:json - the root of the parse tree of JsonElements from which the object is to be deserializedtypeOfT - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection, you should use: Type typeOfT = new TypeToken().getType(); Returns:an object of type T from the json. Returns null if json is null.Throws:JsonSyntaxException - if json is not a valid representation for an object of type typeOfTSince:1.3toStringpublic java.lang.String toString()Overrides:toString in class java.lang.ObjectSkip navigation links