Serialize Object with PersistentCollection To JSON - json

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 !

Related

Send JSON Object Inside MutipartFormData(FormData)

I have Formdata instance in component I use that to append my reactive form data.problem is I have an object that contains this fields {img:File,isCover:boolean} i want to append this to my form data.
problem is if i stringify this object the image field throws empty object {img:{},isCover:true}.when I dig a little bit about it. then saw if we stringify object with methods it will return an empty object
How do I overcome this.i want to send all those data in a single multipart data request.
To achieve what you want you need to append every value of your JSON object to a formdata.
First initialize a variable in you class.
formData = new FormData()
Then in your function append every json object.
this.formData.appen("yourKey", JSON.Object.Value)
It's kind of tedious though... You can , depending on your backend, nest all of the plain text objects in your JSON to a long string and append that to a FormData and call the key "JSON" for instance. And the append the image to the FormData with the key "Image".
After that you can in your API Backend get just the JSON stingified object with the key JSON and do something else with the image by getting it with the key "Image".

Create json array of records with Delphi

I'm trying to create a json with multiple records by following this example: Generate a sample JSON with an array in it in Delphi XE5
must be the same way, except that when I add the array to the object
JSonObj.AddPair (TJSONPair.Create ('records', TJSONArray));
returns the error:
"There is the overloaded version of 'Create' that can be called with arguments These"
How do I add to the array object?
If I convert an array to string and add, to receive the amounts can not treat as an array ...
You're passing it the class reference for a JSON array. You need to pass it an instance.
arr := TJSONArray.Create;
JSONObj.AddPair(TJSONPair.Create('records', arr));
Look carefully at the answers in the question you link to, and you'll see this is exactly what they're doing, too.

How to convert RealmResult to Json using Gson library

In this question How can I serialize a RealmObject to JSON in Realm for Java? The realm representative said that one can serialize realm object through GSON. Can you please explain it how?
I tried this.
RealmResults<Dog> myDogs=realm.where(Dog.class).findAll();
new Gson().toJson(myDogs);
But StackOverflowError occurred.
To make GSON serialization work with Realm you will need to write a custom JsonSerializer for each object that can be serialized and register it as a TypeAdapter.
You can see an example in this gist: https://gist.github.com/cmelchior/ddac8efd018123a1e53a
You get StackOverflow becouse of Gson based on reflection but managed object (RealmObjectProxy) have no real fields and fields of parent is nulls also some of proxy fields produses recursion in field type recognition of Gson it happens in $GsonTypes class.
To serialize RealmObject you can use one of this options:
Write your own adapter for every RealmObject childs which will takes data using getters.
Call realm.copyFromRealm(realmObject) before serialisation. It will looks like new Gson().toJson(realm.copyFromRealm(realmObject))
Use library based on 2nd option RealmSupportForGson
Hope it helps
The easier way is create a List<Dog> with RLMResult<Dog>, and then serialise this List with Gson.
After two days of bug resolve, I found this simple solution:
YourRealmObject realmObj = realm.where(YourRealmObject.class).findFirst();
if(realmObj != null) {
realmObj = realm.copyFromRealm(realmObj); //detach from Realm, copy values to fields
String json = gson.toJson(realmObj);
}

Deserialize a string to json

I want to know whether deserialize converts json to string or string to json.I need my string to be returned as Json so i used deserialize, but unsure about its syntax.Can anyone direct me correctly.
My code
JavaScriptSerializer datajson = new JavaScriptSerializer();
var objec = datajson.Deserialize<string>(data);
return Json(objec,JsonRequestBehavior.AllowGet);
Serialisation is the act of taking objects and turning them into something more persistable or communicable, i.e. turning objects into JSON, XML or binary data.
Deserialisation is the act of taking serialised data and turning it back into objects.
So in your case, if you want to turn your objects/variables into JSON, the process is called serialisation.
Your code, assuming it is MVC C# (you may wish to add these tags to your original post), appears to be deserialising a JSON encoded string into a string, then serialising it back to JSON again when it returns the view. I'm not sure why you would want to do this. You should be able to simply do:
return Json(data, JsonRequestBehavior.AllowGet);

Convert JSON formated String to JsonObject with Jayrock

I have a request parameter in my ASP.NET app. that is in JSON format, and I was wondering if there is a good (quick and easy) way to convert a JSON string to a Jayrocks JsonObject, so I can easily extract key-value pairs without the need to manually parse the string?
Assuming json is the variable containing JSON text, use Jayrock.Json.Conversion.JsonConvert.Import(json). What you will get back in return is either a JsonObject, JsonArray, JsonNumber, System.String, System.Boolean or a null reference depending on the root JSON value in the source JSON text. If you know it is going to be a JSON object for sure then you can safely cast the return value or use JsonConvert.Import<JsonObject>(json).
I would discourage working against JsonObject directly unless you particularly depend on one of its features. You should just pretend the JSON object you get back is a dictionary; either IDictionary or IDictionary<string, object>. With the latest version for .NET Framework 4, you can also work with a JsonObject as a dynamic object.
I don't know Jayrock, but if you want to accept a JSON object as a parameter of Action in MVC2 than the easiest way to do it is by using JsonValueProviderFactory from Futures assembly.
It's part of System.Web.Mvc in MVC3.