Comments (11)
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.
Issue 67 has been merged into this issue.
Original comment by [email protected]
on 28 Mar 2012 at 2:48
from json-simple.
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.
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.
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.
Added additional test case.
Original comment by [email protected]
on 29 Mar 2012 at 4:52
Attachments:
from json-simple.
"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.
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.
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.
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.
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)
- A wrapper for converting A Java HashMap<Object, Object> containing Arrays to a correctly represented JSONObject HOT 1
- JSONParser.parse not working for UTF-8 encoded file HOT 4
- linkedHashMap does not preserve order once data has reached client
- Please provide a source archive for release 1.1.1
- MAVEN, json-simple 1.1.1 depends on Junit HOT 3
- bug in get(), ContainerFactory HOT 1
- JSONObject produces warning in code HOT 6
- Long cast error, even if String used HOT 2
- Patch for /trunk/src/main/java/org/json/simple/parser/ContainerFactory.java HOT 2
- nothing is being converted to a JSONObject in my java class, throwing an exception HOT 1
- Building 1.1.1 with java-1.8.0 tests fail - hash not matching? HOT 2
- Functions are missing from json_simple.jar (1.1)
- should not always use toString() as fallback in JSONValue.writeJSONString() HOT 2
- escaped backslash results in new line character if followed by 'n' or 'r' HOT 1
- JSONParser.parse() throws a parse exception on trailing and leading whitespace of a JSON String. HOT 1
- Make junit's scope to test HOT 3
- Build JSONArray from a Collection HOT 1
- StringBuilder and StringBuffer values are unquoted HOT 1
- I need to modify JSON object value at run time.
- Move project to Github HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-simple.