Git Product home page Git Product logo

Comments (11)

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
Good suggestion. Yes we have quite a few requests on it. May consider it in 
future release.

Original comment by [email protected] on 29 Nov 2011 at 3:20

  • Changed state: Accepted
  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
Issue 67 has been merged into this issue.

Original comment by [email protected] on 28 Mar 2012 at 2:48

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
Keen to hear thoughts on the solution attached for serializing Object[] to 
JSONArray

Changed files only based on read-only checkout.

Original comment by [email protected] on 28 Mar 2012 at 12:28

Attachments:

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
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 [email protected] on 28 Mar 2012 at 2:30

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
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 [email protected] on 29 Mar 2012 at 4:43

Attachments:

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
Added additional test case. 

Original comment by [email protected] on 29 Mar 2012 at 4:52

Attachments:

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
"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 [email protected] on 29 Mar 2012 at 5:04

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
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 [email protected] on 29 Mar 2012 at 7:29

Attachments:

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
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 [email protected] on 2 Apr 2012 at 10:37

Attachments:

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
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 [email protected] on 3 Apr 2012 at 7:40

from json-simple.

GoogleCodeExporter avatar GoogleCodeExporter commented on August 23, 2024
This is done in r211.

Original comment by [email protected] on 10 Aug 2013 at 2:57

  • Changed state: Fixed

from json-simple.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.