I get JSON from a web service which I need to save locally using Core Data. This is part of a sync operation which is performed after certain interval. I need to first convert JSON to NSManagedObject and check if it is already saved locally then just update existing otherwise insert new NSManagedObject.
An NSManagedObject being checked, whether it is saved already or not. can also have relations with other NSManagedObjects (which also need to be part of predicate)
Can anyone suggest any considerable lib to handle this deserialization/serialization from/to JSON <-> NSManagedObject.
i use RestKit for this purposes, it's pretty easy to use
#Eugen
RestKit seems complicated. I had to parse pretty complicated JSON and save in Core data. Also the data on server can change and in next parsing the local Core data values needs to be updated not inserted new.
But on the web service call, I need to authenticate by setting HTTP header user=access_token, password=mypassword.
There is only one method in RestKit to requesting and mapping directly to managedObject and only that method is not working properly while sending the request. I get 403 response. All the other methods which are not related to NSManagedObject authenticates and gets good JSON in the response.
I have wasted so much time trying to make RESTKIT work and now I feel I should try some simple way. Can anyone suggest any good lib or any other suggestions.
Thank you.
Concerning RestKit: I prefer doing web service calls as described here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
As to your deserialisation problem: You might want to convert the NSData object retrieved by the web service to a JSON structure like this:
NSMutableDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
Then you can use https://gist.github.com/pkclsoft/4958148 for populating a NSManagedObject with like this:
Entity *entity = [NSEntityDescription insertNewObjectForEntityForName:#"Entity" inManagedObjectContext:[self managedObjectContext]];
[entity populateFromDictionary:dict];
If the json structure and the structure of the NSManagedObject differ, you can manipulate the NSMutableDictionary before using it for populating the NSManagedOject.
Related
In django view I want to be able to serialize whole context, that is usually used to send to template (typically by calling render and passing locals).
I want to experiment with SPA+API and possibilities to go forward with and I'd like to create function, that would serialize locals to json and return it as json response.
Now problem is, that locals is typically mix of lists, dists and querysets of models.
I can serialize models using django.core.serializers or using django-rest-framework. I can serialize dict with primitive types using json library, but I don't know any simple way how to do mix of those.
Ideal would be way to go through locals dictionary and replace all found models with their serialized representations and then put it all together, maybe even specify before what serializer (in sense of drf) to use for which model. But I really don't want to reinvent wheel in case it already exists.
Anoher question is - is this even a good idea to try to do this? Return json context as alternative to server side rendering? I am in prototyping stage so I am still thinking of how to move forward and any input in the area is appreciated.
I would recommand to go with DRF
ModelSerializer will return a Json encoded array of model
Serializer with DictField will return a Json encoded dict
Serializer with ListField will return a Json encoded list
You can create Serializer with field is another Serializer for nesting purpose.
https://www.django-rest-framework.org/api-guide/fields/#composite-fields
https://www.django-rest-framework.org/api-guide/serializers/#dealing-with-nested-objects
For your' question is this a good idea, i would said :
If you push data to an external source (not django) it's fine
If you push data to django template it's a bad idea, you loose a lot of django power :(
I am looking for either guidance or a good example where I can map data coming from rest services to JSON "type" object which can then be used in a number of different react components.
The JSON Object will be used to map data from a few different rest services, which essentially hold very similar data which makes it better to use one object and then to bind the data to the respective React Components.
I am fairly new to React.JS and I have googled around to find a data mapper to JSON from Rest Service example.
Can anyone help?
You typically don't have to do too much, at least on the front end side. As long as the REST endpoint can return JSON responses you'll be fine. Just make sure you set the appropriate Content-Type headers in the request. Note that setting the header doesn't guarantee a JSON response, the server has to be able to send it in that format.
If you're creating the REST service yourself, you have many options. If you're using node, you can simply return a javascript object. If you're using some other language like Java, C#, etc., they come with libraries that can serialize objects into JSON for you. I use JSON.net when working with C#. In these cases, because the data will be returned as a string, you'll just need to JSON.parse() it upon receiving it and then set it to the appropriate React component's state.
I have a Restlet application already working that accepts JSON and returns JSON entity as response.
I'm trying to understand how I can compress the JSON entity that is returned in the response.
I did not find any clear example on how to achieve it.
I think I have to put somewhere on the router chain the Encoder/EncoderService classes, but I really don't understand where and how to use them.
Could anybody help me?
After some testing, I got the answer.
Creating a new filter like this
Filter encoder = new Encoder(getContext(), false, true, new EncoderService(true));
inside the createInboundRoot() method of my own Application class did the trick, the client requests were already containing the gzip header needed.
I am new to JSP and I am having trouble finding a simple concrete example to make a request to a server that returns a JSON object.
What I am trying to do is something like:
myjson_object = getJSONfrom("my.webserver.com/get/json")
I basically want to add such a line to an existing JSP page so that I can have the JSON object available. I would like to avoid using AJAX or JQuery. I have found several examples like this one, but for some reason they do not work in my case.
Probably trying to do that operation into JSP is not a good idea, since you'd need to use scriptlets and I understand scriptlets are now considered not a good practice (see this)... You'd better do it into a servlet (see this)...
Anyway the code would be similar. You first need to make the request and then parse the JSON response...
In order to make the request you can use HttpURLConnection class. See this question.
Once you have the server response into a BufferedReader, you can parse the JSON using some library, such as Gson. You can find lots of examples of JSON deserializing using Gson in SO, like this or this.
How do I use JSON in java servlet? Using the URL, to pass parameters from a POST servlet, the JSON would respond based on the URL parameters.
You need a JSON library in Java. With that library, you will be able to serialize Java Objects into JSON objects, and send them through HttpServletResponse instance in your Servlet.
"How do you use it"?
JSON is simply one way of representing hierarchical data (such an an object model) in a flat text format (such as an HTTP body, or filesystem file). So you'd use it to represent hierarchical data in these situations.
If you mean how do you parse/create it, there are many mature libraries for JSON handling.
Perhaps a specific situation or use case would assist in pinning down your question, if the above is not the answer you were looking for.