How to send hashmap with a file in GWT - java

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.

Related

Java - Deserialize entire json list from file

This comes from someone who doesn't really know Java.
I have a .json file that contains an array of objects. It is guaranteed that the file is formatted in a correct manner. Is there a simple way to deserialize the entire contents of that file into a List<myObject> (and then serialize it back as an array).
I saw all kinds of code that is more complex than it should, or treats each key in an individual manner, which is something that I don't really need.
My suggestion would be:
in order to get rid off a lot of code and boilerplate use an already developed lib like Gson, Jackson or similar,
take a look how you can model the list you are trying to read... and try to write a POJO Class (online tools can help you to do that)that represents the objects in the list.
try to serialize and deserialize the file's content and
the rest is just enjoy the results...

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.

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.

Ruby object to Java object

I want to create an object from a Ruby program and store it in an SQL database as a blob. I then want to be able to read this blob directly into a Java program at a later date. I understand that these blobs will probably be incompatible. How can I go about making a Java-readable blob from Ruby?
You can use json (xml, yaml) format to store object as string. And parse it in java.
Serialize ruby object to YAML (or JSON, XML etc)
Save it in the DB
Deserialize it from YAML to Java object
You should use thrift, it is much faster than (de)serializing json. See the benchmark here

Reading a Java Object in PHP from a file created with ObjectOutputStream

I'm trying to read a file that was created in a Java-based game using ObjectOutputStream in PHP. The data is a serialized object written in a binary format.
I've been using fopen and fread to get the binary data, but I have absolutely no idea what to do with it.
PHP doesn't understand Java. Both do however understand a common format like JSON, XML, CSV, etc. I'd suggest to change the format to either of them and use that as data transfer format instead.
In case of JSON, you can in Java use Google Gson to convert (encode) fullworthy javabeans into JSON flavor and in PHP you can use json_decode() to convert (decode) it into an associative PHP array.
It doesn't seem easy to reimplement http://download.oracle.com/javase/6/docs/platform/serialization/spec/protocol.html
You can't do it so easily (unless an existing framework is available). This because the binary format used by Java serialization is highly specialized to the JVM, think that there's not guaranteed compatibility even between different JVM versions.
You should use a different approach, for example using XML, YAML or JSON..

Categories

Resources