String Compression: Passing Object Through URL - java

I have two pages, say A and B. User navigates from Page-A to Page-B. Page-B needs to have some values which are passed from the Page-A. Among these values some of them are Objects. Now I have the following ways to pass the parameter to Page-B
Store the Objects in some Scope (say Session, Page-Flow).
Pass the Objects as query string after converting them into String.
The drawbacks of the above two ways are followings respectively:
If User bookmarks the Page-B for later usage and try to access it from different session, it generates Exception. As the Objects are not present in the scope.
There are limitations of the length of the URL, which is 2048 Character (ref). So if I convert it to JSON and try to pass it through URL and if the JSON String is more than the limited characters, I would get JSON Exception from Page-B.
Can I compress the String representation of the Object so that it will not exceed the limitation Character?
How can I solve this issue (by any other means)?
Any solution is greatly appreciated.

Putting serialized objects in the URL is a really bad idea. If you want to access state via URL (GET parameters) then normally the URL should only contain some way of identifying the item in question.
Fortunately if you're using a database back end to persist your objects then the database will usually give them an identity for you, in the form of a primary key. You can then just put the ID in the URL and have Java retrieve the object for that ID whenever it receives a request for it.
If you don't use a database back end then it's up to you to give your objects an identity that they can be located by. The simplest solution would be to store references to the objects in a map and put the map key in the URL.

Related

Store method with its parameter values?

I've a java function like
String abc="myvalue";
myfunction(String abc);
Is there any possibility to store this myfucntion("myvalue"); in an object to recall it automatically?
Explanation:
I've a function with many parameters values may 20-30, my application is being auto-starting when windows is login.
Once user launched the function first time I need to store that function with is passed values for each parameter. So that when an application will be auto-start it can recall that function from its storing place.
Theoretically you could do that using reflection. But that would be well, madness.
You do not store method invocations.
You store data.
You carefully decide who to slice information into reasonable "records", and then you store that data using a decent format, for example JSON or XML. You define a configuration object for example, and then you store the information in that object. And later on, that information is loaded from disk, and then you apply that.

How to get the Object Reference having the hashcode or other Unique String in Java

this is my issue. Im storing Data into a database table which has a column where i store the hashcode (or can be some other Unique String such as an ID because the JVM can re-locate the objects, changing the hashcode). But once i get that String i want to access to the object mapped to that String. I can do it with HashMap like:
ConcurrentHashMap<String, MyClass> MyClassDictionary;
The average of objects to store would be like +800. I can take other options to avoid this kind of things but i really want to know if some of you know a better way than using HashMap.
I found something about a Referenceable Interface that i could implement, you can check it out in the next link:
http://docs.oracle.com/javase/jndi/tutorial/objects/storing/reference.html
Thanks for reading.
You can use any key in the HashMap which is Immutable. String by nature is immutable, which means the object cannot be changed, if someone tries to change the object, a new one will be created and the original remains as it is. So you are safe if you are using unique strings as key. The advantage of using immutable keys in any hashed collection is that, your key object will always be preserved or unchanged. And there will be no chance that someone by mistake and change the key, and leading to a problem that you lose the reference to the value. If the key is not immutable and it is changed from some other place in the code. Then you will never be able to fetch the associated value to that key. This is sometimes refer to as memory leak in java.
The hashCode of an object is very explicitly not unique; it is quite legal for your hashCode() method to just return 0 all the time. You will need to use some other identifier.
You look like you're crossing two separate issues here: Are your objects being stored in the database or just in memory? If they're only in memory, then there's no reason to put the identifier in the database, because the objects will get thrown away when the program restarts. If they're in the database, you need some sort of object-relational mapping solution to recreate Java objects from database rows, and you should look at JPA.

How do I put Serializable Objects in a JSON body? In what format should I store in my database?

I am building a logging/journal service for an osgi framework, with the intention of using my journal entries for restoring the system from a backup after a systemfailure.
But I came across a problem: to make sure I have enough data, necessary to be able to restore the system correctly, I need to pass used functioncalls and their arguments.
I pass the functionName as a String to my journalService and the arguments as an array of Serializable Objects. I demand the arguments to be Serializable because I need to persist them to an external database.
I contact my database via a REST/JSON framework, so I just want to post my journal entries to my database. My problem however is this: how can I put the Serializable Objects (the args) into my JSON body? And in what format do I need to store them in my database?
I would serialize the objects to a byte array/stream, and base64-encode the array/stream to get a printable String.
At the database level, you can store the base64 string as a CLOB, or decode it to a byte array and store it as a BLOB.

Does the sequence of the values matter in a JSON object?

I have a JSON object which I have constructed within my Java program.
JSONObject jObj = {"AAA:aaa","BBB:bbb","CCC:ccc"}
I am sending this object to a server in which it expects the JSON object in the following type.
{"BBB:bbb", "AAA:aaa", "CCC:ccc"}
My question is that does the order of the JSON object really matters on the server side? If yes, how can I change the order?
My question is that does the order of the JSON object really matters on the server side?
It should not matter. According to various JSON specifications, the order of the attributes is not significant. For example:
"An object is an unordered set of name/value pairs." (Source json.org)
"An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array." (Source RFC 7159)
Unfortunately, there are nitwits out there1 who ignore that aspect of the specs, and place some significance on the order of the attributes. (The mistake is usually made when there is a disconnect between the people specifying the APIs and those implementing them, and the people doing the specification work don't really understand JSON.)
Fortunately, the chances are that whoever designed / implemented the server didn't make that mistake. Most Java JSON parsers I've come across don't preserve the attribute order when parsing ... by default2. It would be hard to accidentally implement a server where the order of the JSON attributes being parsed was significant.
If yes, how can i change the order?
With difficulty, I fear:
You could generate the JSON by hand.
There is at least one JSON for java implementation3 that allows you to supply the Map object that holds a JSON object's attributes. If you use a LinkedHashMap or TreeMap, it should retain the insertion order or the lexical order of the attribute keys.
1 - For example, the nitwits that this poor developer was working for ... https://stackoverflow.com/a/4515863/139985
2 - RFC 7159 also says this: "JSON parsing libraries have been observed to differ as to whether or not they make the ordering of object members visible to calling software. Implementations whose behavior does not depend on member ordering will be interoperable in the sense that they will not be affected by these differences.". By my reading, this recommends that JSON libraries should hide any order of the pairs from application code.
3 - JSON-simple : https://code.google.com/p/json-simple/. There could be others too.
IMHO not possible.
JSON docs says
An object is an unordered set of name/value pairs
So the way is getting the values in required order,rather than ordering json
You could use list assuming your server can accept it:
{"list": [ {"AAA":"aaa"},{"BBB":"bbb"},{"CCC":"ccc"}]}
The other answers rightly point out that the order should not matter. There are circumstances were the order may matter in a specific implementation that misunderstands the unordered nature of JSON.
For example say you want take a hash of the JSON string and store the hash for comparison against future hashes. The hash would be different if the order of the fields in the JSON string is not the same the next time you create the hash (even thought the data in the JSON string is the same).
This can happen if you're working with an API or a deserializer that returns JSON strings, with the fields in an inconsistent order.
This question more thoroughly discusses that issue and provides solutions to getting a consistent order JSON order mixed up
The order of fields in a JSON object actually can matter. It depends on the serializer you are using. For example, when you serialize an inherited object you will get an extra JSON field called type=''. When you deserialize it the type field must come before any other JSON Field, otherwise it takes on the type of the parent.

How would I decode this JSON with GSON (multi-d-array)?

I am using GSON to decode JSON strings that are returned from our server. I haven't had any problems, until I ran into this one particular JSON return from the API. The return is in the following format:
"success":1,"errors":[],"data":{"524":{"id":"524"}, "525":{"id":"525"}}
For the other returns I had data as an array of a class of my own creation, but for this return it says that it is an object and not an array. So how should I format my class?
***edit: What I am having trouble with is that the '524' and '525' fields are not static names. They are dependent on what the user's credentials are. There could be fields 323, 324, 325 or a single one 123. It all depends. How would I be able to handle this dynamically?
SOLVED*
What I had to was make 'data' a <String, Object> hashmap in my custom class. Then after the first decoding, I turned 'data' into an array of type Object []. Then for each Object[i], I converted it into a JSON string. After that I used gson.fromJson() to convert it into what I had originally intended for it to be.
If the API is giving inconsistent results and you can't find a reason on your end why it is doing so, one option is to parse the object into a GSON JSONObject o = gson.fromJson(String) and then convert the data to a list if it is not one already by doing o.getElement("data").isList(), etc..
When this is complete, you can then create the object via gson.fromJson(JSONObject,Class). The alternative is to have two classes, one for each instance, but this seems sloppy if this is the only reason to have two different classes.
GSON is correct. From server reply data is object with two members that are objects also. To be array data should have square brackets [] instead of curly brackets {}. More about JSON format here.
Server format was changed or you tried another API version or someone made bug on server side.

Categories