Parsing "ANY" object from/to JSON in *native* Android - android

I've read dozens of post (here and other places) asking for parsing from/to JSON and most people code their exclusive parsers for each class. The usual answer is to use GSON or other libraries but since android supports JSON natively I don't see the point of use third-party libraries.
So I made my own generic JSONParser using Android JDK libraries exclusively. It works fine for any class whose fields are: primitive types, strings, one-dimensional arrays, or other classes. The problem now is to handle arrays of more dimensions. I'm a noob using reflection and JSON, so maybe I misconceived something.
You have to specify the Class you want to convert the JSON string. Then it basically iterates through all the fields in the class and search for a field in JSON with the same name. This works only for public methods (it's not a hard problem for me now, but if someone have an idea to improve it I'll appreciate as well).
My problem comes when I have an array of arrays, I've tried several ways but I'm not able to find a solution to deal with n-dimensional arrays. This is my method for arrays, the "TO-DO" part is the one I don't have a clue for do it. I've tried using the commented line, but it fails. Possibly it's the same problem in both directions, but I'm concerned now in decoding the JSON.
Everything solved!! Go to comments
I think it could be useful for more people, so I've uploaded the whole code here. http://pastebin.com/X7CmaxNf
The method insertArrayFromJSON() is called from public static Object populateObjectFromJSON(Class classname, JSONObject js) {} line 65th.

I've finally solved the problem by myself. I was stucked with this since yesterday because I was facing wrongly the array problem, but then I changed the method to attack it recursively in the right way. In the pastebin is the whole code ( I don't post here cause it's two hundred lines). Feel free for use it and if someone have some ideas to improve it, please tell me!.
http://pastebin.com/Jtf2SLDu
The main point was using Array.set(), Array.newInstance() and Array.get() methods properly. This is in lines 110-125 if someone wants to take a look.
For gain access to the protected&private methods I found the solution in this webiste. So now it works for any class with any public/protected/private field. http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html
I've made in a way that you can set the privacy desired before parsing.

Related

how to read a java object from android resources

I am still new to android and there is probably an obvious answer to my problem. But I have not found the correct question to ask or google for.
What I want to do is read in a xml representation of a (list of) java object(s) and have the objects accessible from my activity.
Is this possible with android resources?
After some reading through the internets the best solution I found was to write the objects as json string and save them in res/values.
And then use something like Gson to parse them as java objects.
But there has to be a better way.
What would be the standard way to approach this?
To add some context: I am programming a small android game and would like to have a research-tree.
Each research item has properties like a name, costs, effects and of course a list of references to other research items as preconditions.
I would appreciate any ideas and suggestions.
If you don't want to use 3rd party solutions, the recommended way is to use the platform's XmlPullParser:
https://developer.android.com/training/basics/network-ops/xml.html

json parsing during download

Is it possible to parse json in parts when it was still downloaded?
For example I load a long json array with a very slow Internet. Can I download a part of text, parse it, add to screen, download next part and so on?
Check out the following SO question it has a few answers, but generally the consensus seems to be that you can depending on the package you are using. I have used GSON (Mentioned in the answers) but have never tried the incremental streaming feature.
GSON makes a lot of things much easier in general though, so I would check that out first if the other things GSON offers seem like a plus to whatever you are trying to do.

Does Android's JSON parser support revivers?

I was trying to usefully answer this question, and figured that the questioner would want to do the conversion of the .Net-style JSON dates ("/Date(######)\/") to dates via a reviver, but looking at the documentation it's not clear to me that Android supports the concept of a "reviver". Does it?
The answer would appear to be: No, not directly. But yes, you can do it (or a form of it) without too much work:
No, in that JSONObject, JSONArray, JSONTokener, JSONReader, etc., don't seem to support the concept directly.
Yes, in that you could probably support a simple form of reviving (such as a context-free substitution of strings in a given format with corresponding Date objects) with little effort via JSONReader, or even (with just a little more work) create a fully-fledged reviver-enabled deserializer (complete with getting the order of things right, etc.). So not built-in, but not from scratch.

Gson on Android: Is it possible to only parse part of the json result, ignoring the rest?

I tried searching for an answer to my question, but I haven't been able so far so I'm hopeing one of you might be able to shed some light on my problem.
For my app I need to parse a JSON response which contains 3 different types of objects, namely Icons, Header and Player.
For now, only the Icons objects are required since the Player and Header classes won't be added untill a later date. So I was wondering, is it possible to parse this response and only focus on the Icons objects, ignoring the other two?
I've run into problems parsing JSON responses before where GSON couldn't parse the data unless everything was correctly labeled so I'm not sure whether or not my idea is even possible.
Could someone shed some light on this for me?
Regards,
Sander
Is jackson parser is an option to you? It is possible with it.

Would you like to integrate a POJO- or Cursor-based library in your android app?

My company wants to publish a library for android, so that other people can integrate our content easily in their custom apps.
Now I am still very uncertain how I should provide the content. Right now I am using POJOs to provide the data in my project.
I was following the debate "POJOs vs. Cursors" which turned out in favor of Cursors. I also have seen an example where somebody stores the parsed resource in a SQLLite-DB and accesses it later through Cursors.
I know that Cursors have many advantages compared to POJOs. But would you like to mess with cursors when you need to implement a library? Some of you guys might have written such libs as well. What did you use and why?
How other libs are doing it...
Facebook seems to use POJOs:
Response format: The server response is a JSON string. The SDK provides a Util.parseJson() method to convert this to a JSONObject, whose fields and values can be inspected and accessed.
We used the Cursor approach in our last project, and found it quite cumbersome. Especially having to check whether there's actually something in the Cursor, iterating over it, finding the right indices for values, closing it properly.. not something I like to do over and over again. Especially the whole index stuff tends to break quite easily, especially since you can't define constants for it if you're actually making use of projections.
A good approach would probably be to use Cursor backed POJOs, at least when it comes to collections. That way the data could be read on demand. You still would have to find a sensible way to close the cursor then, though.
In case of single objects, I say to hell with projections and just dump everything it into a POJO.

Categories

Resources