Kamil-Litwin / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

JSON array type from java.util.Collection or Object[] #48

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
According to the documentation:
http://code.google.com/p/json-simpl/wiki/MappingBetweenJSONAndJavaEntities

JSON arrays are only serialised from java.util.List. When it comes to 
serialisation, I think it might be worth allowing for java.util.Collection 
instead, since the serialisation is done using List.iterator().

On the other hand, serialising Object[] (or any primitive type array) to a JSON 
array would be a very useful addition, too.

Original issue reported on code.google.com by lukas.eder@gmail.com on 30 Jun 2011 at 1:13

GoogleCodeExporter commented 8 years ago
Good suggestion. Yes we have quite a few requests on it. May consider it in 
future release.

Original comment by fangyid...@gmail.com on 29 Nov 2011 at 3:20

GoogleCodeExporter commented 8 years ago
Issue 67 has been merged into this issue.

Original comment by fangyid...@gmail.com on 28 Mar 2012 at 2:48

GoogleCodeExporter commented 8 years ago
Keen to hear thoughts on the solution attached for serializing Object[] to 
JSONArray

Changed files only based on read-only checkout.

Original comment by michael....@gmail.com on 28 Mar 2012 at 12:28

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks for implementing this. ArrayIterator will work, but you could keep your 
implementation a bit more slim by using java.util.Arrays.asList() to convert 
Object[] to List<Object> and then iterating over that List...

Apart from that, what do you think about convenience methods for serialising 
arrays, such as byte[], short[], int[], long[], double[], float[], boolean[], 
char[]?

Cheers
Lukas

Original comment by lukas.eder@gmail.com on 28 Mar 2012 at 2:30

GoogleCodeExporter commented 8 years ago
I did think of using Arrays.asList(), but I wasn't sure it was worth the extra 
overhead as all we want to do is iterate over it. That said, in my testing 
today it seems that the Arrays.asList() code is significantly faster, so I'll 
convert what I've done.

Primitive arrays can't be used with Arrays.asList() in json-simple to maintain 
backwards compatibility with jdks - I believe it requires autoboxing from 1.5.

You can iterate over primitive arrays using reflection, so that's an option. In 
the same test as above (testing with int[] as opposed to Integer[]), the 
primitive iteration using reflection was faster than the object iteration 
regardless.

Based on that, I have an updated version - using Arrays.asList and reflection 
for Primitives. 

Again, keen to hear thoughts. There does seem to be a bit of repetition in 
JSONArray, but based on my limited testing the code seems to run faster this 
way. 

Cheers
Mike

Original comment by michael....@gmail.com on 29 Mar 2012 at 4:43

GoogleCodeExporter commented 8 years ago
Added additional test case. 

Original comment by michael....@gmail.com on 29 Mar 2012 at 4:52

Attachments:

GoogleCodeExporter commented 8 years ago
"I did think of using Arrays.asList(), but I wasn't sure it was worth the extra 
overhead as all we want to do is iterate over it. That said, in my testing 
today it seems that the Arrays.asList() code is significantly faster, so I'll 
convert what I've done."

This seems to be incorrect, using the ArrayIterator *is* faster than 
Arrays.asList(), there was a class loading delay that I didn't take into 
account in my instrumentation.

Original comment by michael....@gmail.com on 29 Mar 2012 at 5:04

GoogleCodeExporter commented 8 years ago
Hi Mike,

You are right, there is a slight overhead when using Arrays.asList() compared 
to when using your ArrayIterator. This is obviously because of the additional 
load on the garbage collector, which has to collect a few more objects. I 
measured an improvement of around 8% when iterating over an array of 6 elements 
100M times (see attachment Test.java)

My output:
Arrays.asList(): 13.688056242
ArrayIterator  : 14.815422996

As far as primitive type arrays are concerned, I think you could overload your 
API method for every primitive type. I personally prefer that for type-safety, 
rather than having "hidden" instanceof checks on a generic Object-type 
argument. Specifically, when overloading a method once with List (raw-type) and 
once with Object, you actually lose some of your API expressiveness... 

So my suggestion is to overload the API several times:

public static String toJSONString(List<?> array);
public static String toJSONString(Object[] array);
public static String toJSONString(byte[] array);
public static String toJSONString(short[] array);
public static String toJSONString(int[] array);
public static String toJSONString(long[] array);

... etc. That is my personal taste (and I'm following some of the JDK's code 
patterns, such as in the java.util.Array class), but your taste may differ :-)

Implementation-wise, you might want to have a look at the various 
java.util.Arrays.toString() methods. For numeric values, you could maybe use 
those? Although, they render an additional whitespace after the comma, so that 
might not be what you need. Anyway, your int[]/long[] serialisation 
implementation can be done a lot more efficiently, as you don't need to escape 
any values...

Cheers
Lukas 

Original comment by lukas.eder@gmail.com on 29 Mar 2012 at 7:29

Attachments:

GoogleCodeExporter commented 8 years ago
I had another look at the various alternatives and drew inspiration from the 
openjdk's implementation of Arrays.toString() (which is only avail in jdk 1.5+) 
to produce this next iteration. 

I've updated the original list implementations to be slightly more efficient, 
and added methods for the various types of primitive arrays and updated 
JSONValue to take advantage of this. 

I'm no longer using the ArrayIterator - using the jdk's for loop pattern was 
faster in my testing. I think the comments should also reference the 
Arrays.toString() method (they're really similar) somehow, but not sure what's 
appropriate. Let me know what you think?

Cheers

mike

Original comment by michael....@gmail.com on 2 Apr 2012 at 10:37

Attachments:

GoogleCodeExporter commented 8 years ago
Hi Mike,

I think this is going the right way! Agreed, for future readers, it would be 
nice to document that the inspiration came from Arrays.toString() methods

Thanks for your work!
Cheers
Lukas

Original comment by lukas.eder@gmail.com on 3 Apr 2012 at 7:40

GoogleCodeExporter commented 8 years ago
This is done in r211.

Original comment by jon.cham...@gmail.com on 10 Aug 2013 at 2:57