-1

Currently forming the JSON using the below string in java , will be used in multithreading environment and values will be dynamic after passing the values this JSON will be posted to the 3 rd party web service

"{"
                        + "  \"test\": {"
                        + "    \"testCont\": {"
                        + "       \"size\": {"
                        + "         \"size\": " + size +","
                        + "         \"number\": " + number
                        + "      }"
                        + "    },"
                        + "    \"testDate\": {"
                        + "      \"testAccount\": {"
                        + "          \"name\": ["
                        +               testMethod.getName()
                        + "          ],"
                        + "        \"school\": {"
                        + "          \"schoolIdentity\": ["
                        +               testMethod.schoolIdentity()
                        + "          ]"
                        + "        }"
                        + "      }"
                        + "    }"
                        + "  }"
                        + "}"

I am aware that using String concatenation will create unnecessary objects in heap

Can some one please tell me the most efficient way to create which will not create additional objects in heap and help to gain performance ?

ravicandy1234
  • 156
  • 1
  • 2
  • 13
  • 1
    Well, apart from tested libraries, you could use a `StringBuilder` and `.append()` JSON text to it. – deHaar Jul 03 '20 at 06:56
  • "I am aware that using String concatenation will create unnecessary objects in heap" - will it tho? From what I know JIT compiler will optimize such code if needed and use StringBuilder behind the scene. And unless you want to concatenate a lot of Strings (a lot = millions of them) it is not worth thinking about optimization. – Amongalen Jul 03 '20 at 07:16
  • 2
    @Amongalen actually, it’s already `javac` which replaces string concatenation (on non-constant values) with `StringBuilder` use. Adjacent constant values are pre-concatenated at compile time anyway. With Java 9, this single expression will get compiled to a single bytecode instruction letting the runtime provide the actual implementation code (including potentially using non-public APIs for better performance). In fact, the expression as posted above *is* already the most efficient approach. – Holger Jul 03 '20 at 09:52
  • 2
    [Recommended read](https://stackoverflow.com/a/54645780/2711488). – Holger Jul 03 '20 at 11:04

1 Answers1

0

IF you are converting from Java POJO you can use

 <dependency> 
        <groupId>com.fasterxml.jackson.core</groupId> 
        <artifactId>jackson-databind</artifactId> 
        <version>2.5.3</version> 
   </dependency> 

This will give you the JASON String

Also you can use

 <dependency> 
       <groupId>com.google.code.gson</groupId> 
       <artifactId>gson</artifactId> 
       <version>2.6.2</version> 
    </dependency> 

Refer this https://www.geeksforgeeks.org/convert-java-object-to-json-string-using-jackson-api/

Mohsin Khan
  • 90
  • 2
  • 11