How to get a simple JSON out of a MongoDb-Document (Java) - java

I work with MongoDb and Jersey in Java. I like to return the Documents I read out of the DB directly as JSON to my AngularJS client. If I call toJson() on a Document I get this BSON representation. That means that a Long for example is represented as an object with a key-value-pair "$numberLong". I don't like this type information in my JSON.
Is there any way to get a simple JSON out of this Document, without converting it to a Java-Object and back? What are the best practices to work with this Documents? Would you include that type information?

Related

Convert XML object to Json using velocity template

I have a use-case where input data is in xml format. It contains multiple fields where we are interested in reading only few fields.
Based on the different use-case, separate velocity templates would have to written to convert xml to json data.
I could not find a better guide on how to write velocity template to convert XML to json. Can someone please help us how to write velocity for conversion from XML to JSON.
Thanks in advance!
First convert your XML into a DTO, and then DTO into Json. You should split your architecture in more than just one layer. The data access layer will handle the WS call and the controller will know how to answer the REST call. Velocity is just a template engine, not a converting tool. You should check Jackson or Gson.

JSON To JSON Transformation | Template Engine for Java

I have a requirement to transform an incoming JSON to an output JSON. For this I am looking for a solution that can work based on templates. What I have in my mind is a solution on lines of XSLT transformation that allows converting an XML to a desired output format (XML, HTML, Text) defined by the style sheet.
One option(or rather a workaround) to use XSLT is to convert JSON to XML that is:
input JSON -> XML -> transform -> output JSON
This approach would have a performance overhead of converting JSON to XML and this would become prominent as the size of incoming object increases.
I found a Node/client layer solution that transforms JSON based on the rules specified in a template. More details about can be found [here][1]. However, I was not able to find any solution that works for a java based application.
Any thoughts/help in terms of solution/frameworks to resolve this would be really helpful.
Thanks.
You could try JOLT, advertised as a JSON to JSON transformation library written in Java.
Or you can search this thread for other libraries and tools which can transform JSON.
The new XSLT 3.0 draft also includes support for JSON as input and output format. Saxon has already started an implementation and seems to support for the JSON part.
You could try JSLT, which is a transform language where you write the fixed part of the output in JSON syntax, then insert expressions to compute the values you want to insert in the template. It's quite similar to how XSLT and XPath work together.
It's implemented in Java on top of Jackson.
For simple transformations you can use jmom library.
For a complex transformation you can use template framework like freemarker.
And convert json data to Map/List form using json library so it can be used by template framework.

Java based JSON manipulations

Is anyone aware of a JSON-XPath style library that allows data manipulation; update, delete, create, etc...
JsonPath.write(json, "$.store.book[*].author", value);
I've looked into the following, but none allow altering the content.
JPath
JSONQuery
JSONiJ
JsonPath (im using 2.2.0) now allows the manipulation of JSON data. e.g.
String jsonData = "{\"drink\":\"juice\"}";
JsonPath.parse(jsonData).set("$.drink", "beer").jsonString();
results in {"drink":"beer"}
JSON was not meant to be a database.
If you want to store your data in the JSON format;
Read the JSON records into your Java application and create data objects.
Modify the data objects in the Java application.
When the application closes, write the JSON records back out.
You'd be better off using an actual database, relational or NoSQL, to store your data, and write JSON records when they're needed.
You might want to have a look at this library I developped to be able to use XML libraries to manipulate JSON: https://github.com/bhabegger/json-n-xml/
It parses JSON to a DOM structure which you can manipulate with standard XML tools and then allows you to serialize back to json.
(JSON may not be meant to be a database but you do have occasions where you just want simple modifications.)
Hope it helps.

How to send hashmap with a file in GWT

I want to know or some code sample which will help me to send the file and the hashmap to the server.
to upload a file to GWt server i use formpanel and a HttpServlet.
this is working fine.
i have a hashmap
private static Map<String, List<Customproperties>> docClass =
new HashMap<String, List<Customproperties>>();
which holds the property of document according to its classname.
I know how to do with RPC. but i want to do with servlet.As i have to upload a file which i have done with servlet. And every Hashmap is related to file .and this file with its property(in HashMap) will send to external repository.
Please help.
There are 2 ways to convert a hashmap to a string (and convert it back to a hashmap)
1: Convert it using JSON library http://json-lib.sourceforge.net/ This will allow you to convert any java object to a JSON string so you can transfer it anywhere. And using the same library or another JSON library, can convert it back to a Java object.
2: Convert it to an XML string using a library called XStream http://x-stream.github.io/ This will convert any Java object into a String represented as XML.
I would recommend to convert your Objects to JSON strings because you are using GWT and it has a lot of support for JSON. And JSON is a good format for Webapps. Another advantage is that other languages can convert your JSON string into an object too.

Provide data to highcharts from a Java method

I have a java method which retrieves data from database and a bean which manages this data.
Using Arrays.toString(var), I get this array [{'tom','1'},{'sawyer','2'}] but highcharts accept data only in this format ['tom','1','sawyer','2']
For now I have to get the array and using replace function to get the correct format but all this has to be done manually.
My question is how to convert the array to the correct format and then pass it to the highcharts data.
How to load a Java method on page load?
Thank you for your patience and help in advance.
Data that needs to be passed to highcharts must be JSON. So the best way to do this is to use a JSON library. http://json.org/java/
Data can be passed also using xml.. The best way to pass data i found is to create a string tn the java class by itself and in the page calling this java method we just need to create an xml dom parser which will convert the string to xml and thus push data to highcharts using jQuery easily.

Categories

Resources