I'm looking forward to deserialize the following JSON object into a single list with built-in Kotlin serialization.
{
items: CustomObject[]
}
#Serializable
data class CustomObject(val name: String)
The thing is, I don't want my list to be accessible through a wrapper object just for the sake of holding the list in a single field, which would end in something like that: CustomObjectList.items[]
Is there a way of specifying to deserialize the items field here directly into a List<CustomObject>? Thank you
Related
I use kotlinx.serialization library to serialize/deserialize JSONs. There is a JSON string:
{"id":"1"}
that can be also represented as
{"uid":"1"}
And I want to handle both names with one property, e.g.:
#Serializable
data class User(val id: String)
Is it possible to parse both JSONs using only one data class and its property?
Yes, you can use the #JsonNames annotation to provide alternative names in addition to the name of the property (see doc). You can also define more than one additional name in the annotation.
#OptIn(ExperimentalSerializationApi::class)
#Serializable
data class User(
#JsonNames("uid")
val id: String,
)
For serialization, the property name will be used. For deserialization, the JSON may contain either the property name or the additional name, both are mapped to the id property.
I want to create an arraylist of type Adapter from a JSON. But since the JSON is not in arraylist format, I'm unable to use gson.fromJson() method.
Is there any way by which I can create a list of my custom object by parsing the following JSON?
JSON data:
"source":{"adapter-config.adapter[0].name":"testAdapter1",
"adapter-config.adapter[0].resolverName":"serviceResolver",
"adapter-config.adapter[0].parameters[0].key":"serviceId",
"adapter-config.adapter[0].parameters[0].value":"serviceIdPathInEvent",
"adapter-config.adapter[0].parameters[1].key":"appId",
"adapter-config.adapter[0].parameters[1].value":"appIdPathEvent",
"adapter-config.adapter[0].parameters[2].key":"env",
"adapter-config.adapter[0].parameters[2].value":"envPathInEvnet"}
My Adapter Object:
public class Adapter {
private String name;
private String resolverName;
private List<KeyValuePair<String, String>> attributeList;
}
Gson does not provide such functionality out of the box. However you can achieve this by manually reading the JSON data from a JsonReader, consuming the JSON property names with nextName() and then parsing them to determine which data they represent. You could either directly read from a JsonReader, or in case the shown JSON data is only an extract from a larger JSON document, you can implement a TypeAdapter for your List<Adapter>. That TypeAdapter could then either be registered with a GsonBuilder by providing new TypeToken<List<Adapter>>() {}.getType() as type, or you could annotate the field holding the List<Adapter> with #JsonAdapter.
For the actual parsing of List<Adapter>, I would recommend storing a current adapter (and its index in the list) in a local variable. Whenever you parse a JSON property name, you could then check if the index encoded in the name is equal to the index of the current adapter, then you are going to modify the existing instance, otherwise if the encoded index is equal to the index of the current adapter + 1 you create a new Adapter instance, add it to the list of adapters and reassign the current adapter variable and its index variable. Then you continue with parsing the remainder of the property name to find out which Adapter field values to set.
(In case you get stuck there, feel free to let me know in the comments and I can try to provide some concrete code; but it would probably be best if you tried it yourself first.)
I'm following a tutorial where one of the tasks is to deserialize json using gson. I looked at several SO posts, but couldn't find one that addressed my issue. I get a string representation of an array in JSON whose fields look like below:
photoJsonArray
[{"id": "28857102437",
"owner":"9457266#N02",
"secret":"a5f02e005f",
"server":"857",
"farm":1,
"title":"",
"ispublic":1,
"isfriend":0,
"isfamily":0,
"url_s":"https:\/\/farm1.staticflickr.com\/857\/28857102437_a5f02e005f_m.jpg" ...and so on
I want to deserialize this list into a list of GalleryItem objects, which only includes the id,url_s and id fields from the JSON array.
I tried the following approach to converting the JSON string to a list of Gallery Item objects, but all the fields ended up with a null value.
Type photoType = new TypeToken<List<GalleryItem>>(){}.getType();
List<GalleryItem>galleryList = g.fromJson(photoJsonArray.toString(),photoType);
I would really appreciate if someone could point me in the right direction!
after some research I discovered that all the fields from the JSON string need to be declared in the class in order to deserialize the JSON into that class. Seems a bit overkill if all you need to use is a couple fields, but hope this helps.
I want to serialize entity object in my Symfony2 application to JSON object (I have to pass it to ajax function and use it in my javascripts).
Everything works fine but I have in my entity object addresses which are PersistentCollection object. Then if I serialize them in normal way field "adresses" has got empty Object. I figured out that I can set "LimitedRecursiveGetSetMethodNormalizer" to "1" and then PersistentCollection is being serialized. The problem is that I've got this object serialized, not array with my addresses so I can't use them in my javascript because they are not there...
\Cloud\ApplicationBundle\Resources\LimitedRecursiveGetSetMethodNormalizer::$limit = 1;
$businessJson = $this->get('serializer')->serialize($business, 'json');
Variable $business is of course Entity Object. I hope that my question is clear.
I have to add that I know that i can convert PersistentCollection object to Array and then serialize it to Json but this way i would have to pass my entity and adresses in seperate variables. I would prefer to do it one variable.
Thanks for any help !
I was wondering if someone could suggest the recommended way to covert flat JSON into a complex java object.
Example JSON
{account_id: 1, user_id:3, user_name:john ... }
But my java class needs to be
class Account {
int account_id;
User user;
}
And here is the user object...
class User {
int user_id;
String user_name;
}
It looks like I could go from JSON to java using the Jackson constructor to create the object the way I need but I also need to covert the java object into a flat JSON.
Do I need to use a serializer/deserializer for each class or is there a way I can do it with simple annotations... By maybe telling it to ignore the user object but not what's inside it..
Let me know what your ideas are. Thanks