Is it an efficient way to serialize JSON data in Java? - java

I don't know whether this question makes sense or not.
I have huge amount of JSON data with me. I am getting that data from Server to the Client side.
Is it good idea to serialize the JSON object in server side ?

I have huge amount of JSON data with me.
Is it good idea to serialize the JSON object in server side ?
No. JSON is already serialized, that's the point of the format.
If you have non-JSON data that you want to deliver to the client then, unless it is a string, you have to serialize it so that it is in a transmittable format.

Check out GSON
Related

Related

What is the better apporach to print a large json data in java 'getOutputStream' or 'getWriter'

Scenario is like i need to print very large json data set. This json data is consumed by mobile application's. In Java service application, used as below to print the json.
response.getWriter().println(mainjson);
getWriter taking too much time to print all the data.
I heard about getOutputStream also. Which is faster in case of large json data?
Any help will be appreciated :-)
It depends on how you retrieve the data and whether your JSON serializer has a streaming api available.
At the moment you are probably operating in three seperate steps
Retrieving all your data
Serializing it to JSON string
Writing the JSON response.
If you are spending a substantial amount of time on the retrieval and serialization part by itself then you can potentially speed things up by using streams. However, this requires your data retrieval and json serializer to support streams.
When using streams, instead of doing everything in sequential steps you basically setup a pipeline that allows you to start writing the response a bit earlier. This is not guaranteed to be faster though, it depends on where your particular bottleneck occurs. If its almost all an issue with the IO to the client then you are not going to see a substantial difference.
Also
Something else to look into is check if your are compressing your response to the user. Gzip can have a substantial impact on the size of text data and may reduce your payload sufficiently to make this a non issue.

How to handle big object sent on socket java?

I am trying to implement a client-server in java
and i made connection between in sockets
and sending JSON objects as strings on streams
if i have big object is there's a way to handle it
so i don't have to regroup it because the limit size of tcp packet (cant know when the single object is fully transferred to me as client or not yet)
note :am using G-son to convert objects to JSON objects
If I have big object is there's a way to handle it so I don't have to regroup it because the limit size of tcp packet. (I cant know when the single object is fully transferred to me as client or not yet)
Actually, the client can know when it has received a complete JSON object. When your client sees the } that matches the opening {, you have the complete object. Of course, this means that that your client needs to understand JSON syntax, but you can use an off-the-shelf JSON parser to do that.
So the best way to do this is for the server to generate and send the JSON, and the client to parse the socket input stream using a normal JSON parser. If you do it that way, then you don't need to know whether the TCP/IP stack has broken the data stream into multiple packets. By the time the JSON parser sees them, they will have been reassembled into a stream of bytes.
If this doesn't answer your question, we need to see what your code is currently doing to generate and send the JSON on the server side.
is there's a way to handle it so i don't have to regroup it because the limit size of tcp packet
You don't have to care about the size of TCP packets. Just write the data. TCP will segmentize and packetize it for you.
(cant know when the single object is fully transferred to me as client or not yet)
Yes you can. You reach the closing '}', as #StephenC mentions. Your JSON parser should take of that for you in any case.
Your question is founded on false assumptions.

Is this possible to send a java object to an obj-c, and obj-c to java via socket?

Ok, just a simple question, I would like to send a object via java and obj-c. Is this possible to do so? Or I need to change the object to string or something first, and convert it back in to the receive side? Thanks.
It is possible to send a serialised Java object to objective C over a socket, but recovering it is difficult since you'd need to write a library to parse the binary data stream. It's possible someone has already written such a library.
It's easier to send objects encoded in JSON or XML, or with Google protocol buffers.
I would like to send a object via java and obj-c
This is the classic problem of communication between different systems/languages.
The solution has always been XML (usually SOAP which is not my taste) and nowadays the options #Joni mentions in his answer

RESTful Web Service Upload/Download Large Data With JSON

What is the best practice if you are implementing web services that will send and receive large files to/from clients. Normally we are sending JSON objects, but it could be problematic if we include large data payload inside of the JSON objects. We need to provide JSON data as well as a payload, anyone have experience with something similar?
You could embed links to the raw data in your JSON responses. For example:
{
title: 'A Really Big File',
date: '2011-11-11',
file: 'http://example.com/really_big_file.xls'
}
That way you can allow clients to decide whether or not they want to dereference the big file or not.
Base64 is a very inefficient way of doing this, but universal. You could send your files using HTTP Post-request with special parameter "multipart/form-data".

Twitter saved JSON String to Java Object (POJO) using something like twitter4j..?

I have a large collection of twitter messages from the streaming twitter API saved as JSON strings in text files.
I wanted to know if anyone knew how I could convert these JSON strings into something like the Twitter4J status object for use as a simple object with getters and setters?
I was thinking about debugging the source and writing my own inject class which would mimic the input stream classes, however I wonder if there is a better way..?
Thanks!
Try DataObjectFactory#createStatus(String). It's a simple static method that returns one single twitter4j.Status object.
http://twitter4j.org/en/javadoc/twitter4j/json/DataObjectFactory.html#createStatus(java.lang.String)
You can try using Google's Protobuff or Codehause's XStream or Jackson
This thread might help
https://stackoverflow.com/questions/338586/a-better-java-json-library
Depends on what you want to do with the data. One thought that comes to mind is importing it into a database like MongoDb which already has support for importing JSON http://www.mongodb.org/display/DOCS/Import+Export+Tools . then you can proceed to analyse or convert the data further from there

Categories