Deserialise a Json object containing a Json String - parsing

I have a problem where I am unable to de-serialise a Json string to an object in cases where the object already has a Json string in it.
Ex:
Class A {
int a;
String jsonRpresenationOfSomeObject;
// other stuff
}
I am unable to de-serialise an object of the above Class A. I get an exception for the field "jsonRpresenationOfSomeObject"
Custom de-serialiser is not an option for my use case. Am looking for something generic.
Also found no luck with the Gson library.
Would appreciate any help on this.

There is no issue in serializing and deserializing in JSON for the above mentioned scenario of yours.
I have tried and found that the JSON is able to deserialize your class properly.
i think their might be problem with Access-Specifier of your class.
If you still face the problem, then prvide us the sample source.

Related

How to use inheritance with Swift and ObjectMapper?

I'm new to Swift and I have to serialize an object structure into a JSON string in my iOS( iOS >= 8) project.
So I decided to use the ObjectMapper library (I integrated it with Cocoapod).
But my problem is that the structure is the following.
ObjectA can have a list of children objects as an array stored in an instance variable.
Objects that can be stored in the instance array can be of multiple types, say ObjectB and ObjectC. So, in Java with GSON I would have created an InterfaceD and made both of my classes implement it and made the array in ObjectA store InterfaceD types, but I can't figure how to do this with Swift object model as it results in empty {} JSON objects.
The resulting JSON should look like this.
{"children":[
{"type":"ObjectB", "value1":"foo"},
{"type":"ObjectC", "value1":"bar", "value2":"baz"}
]}
and I get
{"children":[
{},
{}
]}
Notice that the two entries that have to be serialized from objectA and ObjectC should have different structures.
I tried multiple things but each times I'm stuck in a dead end.
I tried using generics, tried to use Mappable protocol as my array type, I tried classic class inheritence but any one failed.
Have you any idea how I can achieve this ?
Note that I know that I could add a serialization function to each object and retrieve the strings recursively and concatenate them. But I really want to avoid implementing a JSON serializer by myself as I already use on successfully as Alamofire is already used in the project). Also implementing a new serializer is error prone and not a clean way to solve the problem IMO.
Thanks a lot.
I never had the solution but as a workaround I just made my objects produce a dictionnary from all its values. Then I recursively add child objects dictionnaries as the current dictionnary values.
It adds a toDict() function to each object that I forced with a protocol.
When the resulting object is only made of a tree of dictionnaries, the ObjectMapper serialization works fine...

converting string to json parsing directly

Can any one suggest me a way to parse string to json.String variable is holding the json data (exact json data).I want to directly return that string to my kendo grid whic take only json data.Is there a way to directly parse the string to json.
Thanks in advance
Take a look at this library, I believe that will help.
Json.NET http://james.newtonking.com/json/help/index.html

In AFNetworking's JSONRequestOperationWithRequest:success:failure: method, what type of object is the JSON response?

It gives you a variable called JSON of type id, but how do I manipulate this? Is it a string? Do I have to serialize it first? How exactly do I interact with it?
It is returned in the form of dictionary, you just need to extract the value based on the key. Example :
[JSON valueForKey:#"key"];
You can get more than the basics
here
Fantastic course, there's very good code about json handling in the photo mania app.

Traverse through JSon data in dart?

After trying a lot sorry for asking such a trivial question.
Given below screenshot consist of a data that I have successfully received from the server.
I would like to know how to traverse through the data since whenever I try to cast it to something and try a foreach it gives an error.
The actual data sent from the server is a List() type.
I want to know how to cast it to same type and use it here.
I tried casting but it says unexpected token here.
Any help is appreciated.
JSON.parse return type depends on the String you try to parse. See the doc :
Parses json and build the corresponding parsed JSON value.
Parsed JSON values are of the types num, String, bool, Null, Lists of parsed JSON values or Maps from String to parsed JSON values.
From your screenshot, it seems that the return value is a List. You can do something like the following to use it (did you notice the typo in your commented code - .fore) :
final parsedList = JSON.parse(e.data)/*.fore*/;
parsedList.forEach((x){
query('#idData').appendText(x[0]);
query('#idData').appendText(x[1]);
query('#idData').appendText(x[2]);
query('#idData').appendText(x[3]);
});

Grails JSON Converters and transient properties

Using Grail 1.3.7 I found that the JSON converter ignores transient properties of Domain objects.
Question: Is there an elegant way to work around this obstacle.
Bonus question: what's the reasoning behind excluding calculated fields(transient props) from being sent to the response????
what works for me is this one line
def jsonobj=domobj.properties as JSON
one way would be to manually create your json response, e.g.
["prop1" : obj.prop1, "prop2" : obj.prop2, ...] as JSON
Transient is made exactly for that: Variables may be marked transient to indicate that they are not part of the persistent state of an object
And JSON is an serialized (=persistent) state of object
So, if you need it to be serialized - you have to create an new class, just for json serialization, that will have all fields you need to serialize.
If you need fine-grained control over the fields that are included/excluded in the JSON, I fine using the JSONBuilder a better option than the converter. Here's an example of how to do this.
You could use the "marshallers" plug-in and define you transient property as virtual like this:
static marshalling = {
virtual {
yourPropery { value, json -> json.value(value.yourPropery) }
}
}

Resources