question

Upvotes
Accepted
3 2 1 5

org.json.JSONOrderedObject Java library

Hi,

First of all, this is my first JSON in Java experience.

I was getting 400 - bad request for extraction call (/Extractions/Extract). Then I've tried to sent just a json string with property order exactly as it was in the example and it was working. A bit tricky.

I don't think my library (com.googlecode.json-simple) can maintain the property order within a JSONObject. In the API's Java examples there's a org.json.JSONOrderedObject - I was not able to find out which JSON library contains this class.

Thanks for any advice.

Filip

dss-rest-apidatascope-selectdssjavajsonjar
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
Accepted
3 2 1 5

Ok, now I see these classes are included within the examples.

Anyway I was hoping for more "Maven" way - I don't feel comfortable neither including this 3rd party to my source code nor creating and deploying artifact from those.

I've resolved the issue with this simple work-around, still using json-simple, but substituting standard HashMap with LinkedHashMap.

public class JSONOrderedObject extends LinkedHashMap<String, Object> implements Map<String, Object>, JSONAware, JSONStreamAware {
                

@Overridepublic String toJSONString() {
return JSONObject.toJSONString(this);}

@Overridepublic void writeJSONString(Writer writer) throws IOException {
JSONObject.writeJSONString(this, writer);}
}
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I have the same problem: I would like use something more "Maven".

I found org.springframework.boot.configurationprocessor.json.JSONObject which internally uses a LinkedHashMap.

maven dependency:

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-configuration-processor</artifactId>

<version>2.3.12.RELEASE</version>


However I was wondering if anyone else is using something different and why.

Upvote
13.7k 27 8 12

@filip.balas

If you download the Java code examples from the downloads tab, you will find the libraries we used, under \DSS2_Java_Examples\src\org\json.

Note these Java samples are currently being rewritten to make them easier to understand. The first one in the new style is DSS2SearchFirRICClientV2.java. The others will come soon.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sample rewrite is finished, the new versions are now available for download here.

Upvotes
9.6k 10 7 7

Based on JSONOrderedObject source code, it seem that the class is written using pure java classes(no using any JSON library classs) as shown below:


import.png (19.3 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
582 10 15 23

@filip.balas

Thanks for sharing your work around solution. That's great! The org.json.JSONOrderedObject class is a modified version of the org.json.JSONObject, to help maintain the order of the properties in the JSON request object (yes, as you noted, the exact order of the properties is required for most of the requests). The main difference is the org.json.JSONOrderedObject class uses LinkedHashMap instead on Map.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.