I was solving a question on Hackerrank where I had to hit an API which was returning Json, then I had to map into Object. I had used Libraries like Gson and ObjectMapper to solve the question but the Hackerank IDE was not able to resolve Gson and ObjectMapper libraries even after adding gson and Object mapper libraries in import section. Is there any way to add dependencies in Hackerrank IDE and successfully submit it?
According to the HackerRank Environment documentation, the following libraries are available:
TestNG, JSON Simple, Tagsoup, Google/Gson, HTTPCore, HTTPClient, XMLResolver, Jing, JUnit, Hamcrest, Commons Lang3, Commons Logging.
Source: https://support.hackerrank.com/hc/en-us/articles/1500002392722-Execution-Environment. (Click on "Coding and Database" and scroll to the Java information ...)
Well, you are not allowed to use libraries or frameworks in these challenges. The idea is to check how you would solve the problem, not the developer of a library. That's why these lessons are so challenging. Back to the roots: Use just the Java API itself to pass!
Related
Sorry for my naive question, I am looking for some tools that would automatically convert JSON objects (that are generated in swagger UI) to a Java class objects in Eclipse.
Are there any such tools or plugins that can integrate swagger code with Eclipse Java and Groovy Grails framework. It would save a lot of time by not rewriting a huge amount of Java code every time when I want to change something in JSON objects.
I suggest to use autorest tool to generate code and models. It supports multiple languages including Java. Here is the github repository autorest
Just follow simple steps-
npm install -g autorest
autorest --input-file=https://******url******/swagger/v1/swagger.json --java --namespace=****namespace*****
You can use Google Gson Library in java.
You could use that or Groovys JSONsluprer but I depending on what you're trying to achieve it might just be easiest if you have a swagger to start with swagger codegen https://github.com/swagger-api/swagger-codegen tools and have it make those for you. If not you can always look to investigate how they did it.
This is a duplicate of this question, which was written in 2010. Now that things may have changed in 2017, I think this is a great opportunity to re-ask the question.
What libraries are needed?
Using org.restlet.jee 2.3.10, I have noticed that there are several libraries which could support JSON. Here are some names from the Maven Repository:
org.restlet.ext.jackson
org.restlet.ext.json
org.restlet.lib.org.restlet.lib.org.json
org.restlet.lib.org.json
What is the current recommended / standard way of GET-ing and POST-ing JSON-formatted text?
It seems like: "org.restlet.JSON".
You can see the official tutorial here.
I use the org.json.* classes which are part of the j2objc distribution. The mapping of my DTO classes and the JSON Objects are made by hand so far.
I know there is GSOn and Jackson.
Is there a JSON to Object Mapper library which translates well with J2objc and works well in the translated Objective-C code?
Jackson and GSON are the two best libraries, and both have been translated using j2objc for iOS apps from different teams (no public ports that I'm aware of, though).
If you control the server-side of the app, though, protocol buffers are generally faster than any JSON->Java alternative. That is why they're used by most of Google's iOS apps, including Inbox and Sheets that use j2objc. j2objc now publicly supports protocol buffers.
To convert json data to java object you can use:
Jackson
Gson
Follow this link or this link for nice GSON tutorial.
I personally used GSON, and it is best library for json to java object conversion.
I don't know about conversion part of j2objc but you can follow this answer, if you want to do similar thing with iOS code
Edit
According to this answer, j2objc requires source code for conversion. You will get full source code from here.
Now, the important part, Gson can be converted to j2objc and you can find test repository to test support for gson-2.3 with j2objc here.
I am using the Jackson jars (core 2.3.1, annotations 2.2.3, databind 2.3.1, all the latest from the downloads page) to read a Json file, grab what I need put it into a new object and write/printout that object to a file or the screen.
I have seen most examples say that it's as easy as
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(object);
Or
ObjectWriter mapper = new ObjectMapper().writer().withDefaultPrettyPrinter();
writer.writeValueAsString(object);
Although I have the necessary jar's I keep getting the error
ClassNotFoundException: com.fasterxml.jackson.annotation.JsonPropertyDescription.
Upon digging deeper I find that the JsonPropertyDescription class that is in the github for jackson, is not packaged with the annotations 2.2.3 jar.
So has it been deprecated and there is some new way I haven't seen to take an object and print it to json, or was this class accidentally left out of the newer version?
Not sure if you've solved this already by updating your jars but here go some answers:
Upon digging deeper I find that the JsonPropertyDescription class that is in the github for jackson, is not packaged with the annotations 2.2.3 jar.
It's a new feature in 2.3.
Is there some new way I haven't seen to take an object and print it to json?
I don't think so: writeValueAsString() 2.3.3 (it's not deprecated).
It looks like someone had a similar problem and the solution they suggested is basically "update your jars". The asker didn't reply so we don't know whether that worked or not.
I'm used to PHP, and decoding json data is just a line of code.
What would be the easiest way to do this in java?
Pick one of the libraries from the Java section at the bottom of the json.org page.
Gson
Userguide
have a look at
http://code.google.com/p/json-simple/
maybe it helps ;-)
I love Gson, it's very simple and easy to use. If you are interessted in more, here is a tutorial (german): http://blog.mynotiz.de/programmieren/java-json-decode-tutorial-2074/
Decoding json in java is not too hard. Google's gson api handles json data very well. A tutorial on decoding json data using gson is there in my blog http://preciselyconcise.com/apis_and_installations/json_to_java.php
I like Flexjson. It's lightweight and easy to use.
But I admit that I haven't bothered to compare all the alternatives :-)
There are many JSON libraries available in Java.
The most notorious ones are: Jackson, GSON, Genson, FastJson and org.json.
There are typically three things one should look at for choosing any library:
Performance
Ease of use (code is simple to write and legible) - that goes with features.
For mobile apps: dependency/jar size
Specifically for JSON libraries (and any serialization/deserialization libs), databinding is also usually of interest as it removes the need of writing boiler-plate code to pack/unpack the data.
For 1, see this benchmark: https://github.com/fabienrenaud/java-json-benchmark I did using JMH which compares (jackson, gson, genson, fastjson, org.json, jsonp) performance of serializers and deserializers using stream and databind APIs.
For 2, you can find numerous examples on the Internet. The benchmark above can also be used as a source of examples...
Quick takeaway of the benchmark: Jackson performs 5 to 6 times better than org.json and more than twice better than GSON.
Let me know if you have any questions.