Last active
November 1, 2025 09:45
-
-
Save webfirmframework/824d3408c4beb4845cd32f5b43ff5eee to your computer and use it in GitHub Desktop.
JSON parsing/building examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.webfirmframework.wffweb.json.*; | |
| import java.time.LocalDate; | |
| public class JsonParserUsage { | |
| private static void jsonObjectParsingExample1() { | |
| //To parse JSON object | |
| JsonMap jsonMap = JsonMap.parse(""" | |
| {"key1" : "value1"} | |
| """); | |
| //To build JSON string | |
| String jsonString = jsonMap.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To put key-value | |
| jsonMap.put("key2", "value2"); | |
| //To build JSON string quickly using parallel stream, useful only if the JsonMapNode is very huge. | |
| jsonString = jsonMap.toBigJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value by key | |
| String valueForKey1 = jsonMap.getValueAsString("key1"); | |
| System.out.println("valueForKey1 = " + valueForKey1); | |
| JsonMapNode jsonMapNode = jsonMap; | |
| JsonBaseNode jsonBaseNode = jsonMapNode; | |
| } | |
| private static void jsonObjectParsingExample2() { | |
| //To parse JSON object | |
| JsonConcurrentMap jsonMap = JsonConcurrentMap.parse(""" | |
| {"key1" : "value1"} | |
| """); | |
| //To build JSON string | |
| String jsonString = jsonMap.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To put key-value | |
| jsonMap.put("key2", "value2"); | |
| //To build JSON string quickly using parallel stream, useful only if the JsonMapNode is very huge. | |
| jsonString = jsonMap.toBigJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value by key | |
| String valueForKey1 = jsonMap.getValueAsString("key1"); | |
| System.out.println("valueForKey1 = " + valueForKey1); | |
| JsonMapNode jsonMapNode = jsonMap; | |
| JsonBaseNode jsonBaseNode = jsonMapNode; | |
| } | |
| private static void jsonObjectParsingExample3() { | |
| //To parse JSON object | |
| JsonLinkedMap jsonMap = JsonLinkedMap.parse(""" | |
| {"key1" : "value1"} | |
| """); | |
| //To build JSON string | |
| String jsonString = jsonMap.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To put key-value | |
| jsonMap.put("key2", "value2"); | |
| //To build JSON string quickly using parallel stream, useful only if the JsonMapNode is very huge. | |
| jsonString = jsonMap.toBigJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value by key | |
| String valueForKey1 = jsonMap.getValueAsString("key1"); | |
| System.out.println("valueForKey1 = " + valueForKey1); | |
| JsonMapNode jsonMapNode = jsonMap; | |
| JsonBaseNode jsonBaseNode = jsonMapNode; | |
| } | |
| private static void jsonObjectParsingByJsonParserExample1() { | |
| JsonParser jsonParser = JsonParser.newBuilder() | |
| .jsonObjectType(JsonObjectType.JSON_MAP) | |
| .jsonArrayType(JsonArrayType.JSON_LIST).build(); | |
| //To parse JSON object | |
| JsonMap jsonMap = jsonParser.parseJsonObject(""" | |
| {"key1" : "value1"} | |
| """) instanceof JsonMap parsedJsonMap ? parsedJsonMap : null; | |
| //To build JSON string | |
| String jsonString = jsonMap.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To put key-value | |
| jsonMap.put("key2", "value2"); | |
| //To build JSON string quickly using parallel stream, useful only if the JsonMapNode is very huge. | |
| jsonString = jsonMap.toBigJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value by key | |
| String valueForKey1 = jsonMap.getValueAsString("key1"); | |
| System.out.println("valueForKey1 = " + valueForKey1); | |
| JsonMapNode jsonMapNode = jsonMap; | |
| JsonBaseNode jsonBaseNode = jsonMapNode; | |
| } | |
| private static void jsonObjectParsingByJsonParserExample2() { | |
| JsonParser jsonParser = JsonParser.newBuilder() | |
| .jsonObjectType(JsonObjectType.JSON_CONCURRENT_MAP) | |
| .jsonArrayType(JsonArrayType.JSON_LIST).build(); | |
| //To parse JSON object | |
| JsonConcurrentMap jsonMap = jsonParser.parseJsonObject(""" | |
| {"key1" : "value1"} | |
| """) instanceof JsonConcurrentMap parsedJsonMap ? parsedJsonMap : null; | |
| //To build JSON string | |
| String jsonString = jsonMap.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To put key-value | |
| jsonMap.put("key2", "value2"); | |
| //To build JSON string quickly using parallel stream, useful only if the JsonMapNode is very huge. | |
| jsonString = jsonMap.toBigJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value by key | |
| String valueForKey1 = jsonMap.getValueAsString("key1"); | |
| System.out.println("valueForKey1 = " + valueForKey1); | |
| JsonMapNode jsonMapNode = jsonMap; | |
| JsonBaseNode jsonBaseNode = jsonMapNode; | |
| } | |
| private static void jsonObjectParsingByJsonParserExample3() { | |
| JsonParser jsonParser = JsonParser.newBuilder() | |
| .jsonObjectType(JsonObjectType.JSON_LINKED_MAP) | |
| .jsonArrayType(JsonArrayType.JSON_LIST).build(); | |
| //To parse JSON object | |
| JsonLinkedMap jsonMap = jsonParser.parseJsonObject(""" | |
| {"key1" : "value1"} | |
| """) instanceof JsonLinkedMap parsedJsonMap ? parsedJsonMap : null; | |
| //To build JSON string | |
| String jsonString = jsonMap.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To put key-value | |
| jsonMap.put("key2", "value2"); | |
| //To build JSON string quickly using parallel stream, useful only if the JsonMapNode is very huge. | |
| jsonString = jsonMap.toBigJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value by key | |
| String valueForKey1 = jsonMap.getValueAsString("key1"); | |
| System.out.println("valueForKey1 = " + valueForKey1); | |
| JsonMapNode jsonMapNode = jsonMap; | |
| JsonBaseNode jsonBaseNode = jsonMapNode; | |
| } | |
| private static void jsonArrayParsingExample1() { | |
| //To parse JSON text | |
| JsonList jsonList = JsonList.parse(""" | |
| ["item1", "item2", "item3"] | |
| """); | |
| //To add value | |
| jsonList.add("item4"); | |
| //To build JSON string | |
| String jsonString = jsonList.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value from 1st index | |
| String valueFromIndex1 = jsonList.getValueAsString(1); | |
| System.out.println("valueFromIndex1 = " + valueFromIndex1); | |
| JsonListNode jsonListNode = jsonList; | |
| JsonBaseNode jsonBaseNode = jsonListNode; | |
| } | |
| private static void jsonArrayParsingExample2() { | |
| //To parse JSON text | |
| JsonLinkedList jsonList = JsonLinkedList.parse(""" | |
| ["item1", "item2", "item3"] | |
| """); | |
| //To add value | |
| jsonList.add("item4"); | |
| //To build JSON string | |
| String jsonString = jsonList.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value from 1st index | |
| String valueFromIndex1 = jsonList.getValueAsString(1); | |
| System.out.println("valueFromIndex1 = " + valueFromIndex1); | |
| JsonListNode jsonListNode = jsonList; | |
| JsonBaseNode jsonBaseNode = jsonListNode; | |
| } | |
| private static void jsonArrayParsingByJsonParserExample1() { | |
| JsonParser jsonParser = JsonParser.newBuilder() | |
| .jsonObjectType(JsonObjectType.JSON_MAP) | |
| .jsonArrayType(JsonArrayType.JSON_LIST).build(); | |
| //To parse JSON text | |
| JsonList jsonList = jsonParser.parseJsonArray(""" | |
| ["item1", "item2", "item3"] | |
| """) instanceof JsonList parsedJsonList ? parsedJsonList : null; | |
| //To add value | |
| jsonList.add("item4"); | |
| //To build JSON string | |
| String jsonString = jsonList.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value from 1st index | |
| String valueFromIndex1 = jsonList.getValueAsString(1); | |
| System.out.println("valueFromIndex1 = " + valueFromIndex1); | |
| JsonListNode jsonListNode = jsonList; | |
| JsonBaseNode jsonBaseNode = jsonListNode; | |
| } | |
| private static void jsonArrayParsingByJsonParserExample2() { | |
| JsonParser jsonParser = JsonParser.newBuilder() | |
| .jsonObjectType(JsonObjectType.JSON_MAP) | |
| .jsonArrayType(JsonArrayType.JSON_LINKED_LIST).build(); | |
| //To parse JSON text | |
| JsonLinkedList jsonList = jsonParser.parseJsonArray(""" | |
| ["item1", "item2", "item3"] | |
| """) instanceof JsonLinkedList parsedJsonList ? parsedJsonList : null; | |
| //To add value | |
| jsonList.add("item4"); | |
| //To build JSON string | |
| String jsonString = jsonList.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value from 1st index | |
| String valueFromIndex1 = jsonList.getValueAsString(1); | |
| System.out.println("valueFromIndex1 = " + valueFromIndex1); | |
| JsonListNode jsonListNode = jsonList; | |
| JsonBaseNode jsonBaseNode = jsonListNode; | |
| } | |
| private static void jsonParsingWithCustomJsonMapAndCustomJsonArrayExample() { | |
| //You can implement more methods to extend the functionality of exiting JsonMap, JsonList etc.. as follows | |
| class CustomJsonMap extends JsonMap { | |
| public LocalDate put(final String key, final LocalDate localDate) { | |
| final JsonList dateParts = new JsonList(3); | |
| dateParts.add(localDate.getYear()); | |
| dateParts.add(localDate.getMonthValue()); | |
| dateParts.add(localDate.getDayOfMonth()); | |
| return super.put(key, dateParts) instanceof JsonList existingDateParts ? LocalDate.of(existingDateParts.getValueAsInteger(0), existingDateParts.getValueAsInteger(1), existingDateParts.getValueAsInteger(2)) : null; | |
| } | |
| public LocalDate getValueAsLocalDate(final String key) { | |
| final JsonListNode dateParts = super.getValueAsJsonListNode(key); | |
| return dateParts != null ? | |
| LocalDate.of( | |
| dateParts.getValueAsInteger(0), | |
| dateParts.getValueAsInteger(1), | |
| dateParts.getValueAsInteger(2)) | |
| : null; | |
| } | |
| } | |
| class CustomJsonList extends JsonList { | |
| public boolean add(final LocalDate localDate) { | |
| final JsonList dateParts = new JsonList(3); | |
| dateParts.add(localDate.getYear()); | |
| dateParts.add(localDate.getMonthValue()); | |
| dateParts.add(localDate.getDayOfMonth()); | |
| return super.add(dateParts); | |
| } | |
| public LocalDate getValueAsLocalDate(final int index) { | |
| final JsonListNode dateParts = super.getValueAsJsonListNode(index); | |
| return LocalDate.of( | |
| dateParts.getValueAsInteger(0), | |
| dateParts.getValueAsInteger(1), | |
| dateParts.getValueAsInteger(2)); | |
| } | |
| } | |
| //CUSTOM_JSON_MAP and CUSTOM_JSON_LIST are new, it is available since 12.0.6. | |
| JsonParser jsonParser = JsonParser.newBuilder() | |
| .jsonObjectType(JsonObjectType.CUSTOM_JSON_MAP).jsonMapFactory(CustomJsonMap::new) | |
| .jsonArrayType(JsonArrayType.CUSTOM_JSON_LIST).jsonListFactory(CustomJsonList::new).build(); | |
| //To parse JSON object | |
| CustomJsonMap jsonMap = jsonParser.parseJsonObject(""" | |
| {"key1" : "value1", | |
| "valuesArray": ["one", "two", "three"] | |
| } | |
| """) instanceof CustomJsonMap customJsonMap ? customJsonMap : null; | |
| //To build JSON string | |
| String jsonString = jsonMap.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To put key-value | |
| jsonMap.put("key2", "value2"); | |
| jsonMap.put("dateOfBirth", LocalDate.of(2014, 1, 14)); | |
| CustomJsonList arrayOfDates = new CustomJsonList(); | |
| arrayOfDates.add(LocalDate.of(2015, 2, 15)); | |
| arrayOfDates.add(LocalDate.of(2016, 3, 16)); | |
| arrayOfDates.add(LocalDate.of(2017, 4, 17)); | |
| jsonMap.put("arrayOfDates", arrayOfDates); | |
| //To build JSON string quickly using parallel stream, useful only if the JsonMapNode is very huge. | |
| jsonString = jsonMap.toBigJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| jsonMap = jsonParser.parseJsonObject(jsonString) instanceof CustomJsonMap customJsonMap ? customJsonMap : null; | |
| //To get value by key | |
| String valueForKey1 = jsonMap.getValueAsString("key1"); | |
| System.out.println("valueForKey1 = " + valueForKey1); | |
| //To get value as LocalDate by custom method | |
| LocalDate dateOfBirth = jsonMap.getValueAsLocalDate("dateOfBirth"); | |
| System.out.println("dateOfBirth = " + dateOfBirth); | |
| JsonMapNode jsonMapNode = jsonMap; | |
| JsonBaseNode jsonBaseNode = jsonMapNode; | |
| //To get valuesArray | |
| CustomJsonList jsonList = jsonMap.getValueAsJsonListNode("valuesArray") instanceof CustomJsonList customJsonList ? customJsonList : null; | |
| //To add value | |
| jsonList.add("item4"); | |
| //To build JSON string | |
| jsonString = jsonList.toJsonString(); | |
| System.out.println("jsonString = " + jsonString); | |
| //To get value from 1st index | |
| String valueFromIndex1 = jsonList.getValueAsString(1); | |
| System.out.println("valueFromIndex1 = " + valueFromIndex1); | |
| JsonListNode jsonListNode = jsonList; | |
| jsonBaseNode = jsonListNode; | |
| arrayOfDates = jsonMap.getValueAsJsonListNode("arrayOfDates") instanceof CustomJsonList customJsonList ? customJsonList : null; | |
| LocalDate dateFromIndex0 = arrayOfDates.getValueAsLocalDate(0); | |
| System.out.println("dateFromIndex0 = " + dateFromIndex0); | |
| LocalDate dateFromIndex1 = arrayOfDates.getValueAsLocalDate(1); | |
| System.out.println("dateFromIndex1 = " + dateFromIndex1); | |
| LocalDate dateFromIndex2 = arrayOfDates.getValueAsLocalDate(2); | |
| System.out.println("dateFromIndex2 = " + dateFromIndex2); | |
| } | |
| public static void main(String[] args) { | |
| jsonObjectParsingExample1(); | |
| jsonObjectParsingExample2(); | |
| jsonObjectParsingExample3(); | |
| jsonObjectParsingByJsonParserExample1(); | |
| jsonObjectParsingByJsonParserExample2(); | |
| jsonObjectParsingByJsonParserExample3(); | |
| jsonArrayParsingExample1(); | |
| jsonArrayParsingExample2(); | |
| jsonArrayParsingByJsonParserExample1(); | |
| jsonArrayParsingByJsonParserExample2(); | |
| jsonParsingWithCustomJsonMapAndCustomJsonArrayExample(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jsonString = {"key1":"value1"} | |
| jsonString = {"key1":"value1","key2":"value2"} | |
| valueForKey1 = value1 | |
| jsonString = {"key1":"value1"} | |
| jsonString = {"key1":"value1","key2":"value2"} | |
| valueForKey1 = value1 | |
| jsonString = {"key1":"value1"} | |
| jsonString = {"key1":"value1","key2":"value2"} | |
| valueForKey1 = value1 | |
| jsonString = {"key1":"value1"} | |
| jsonString = {"key1":"value1","key2":"value2"} | |
| valueForKey1 = value1 | |
| jsonString = {"key1":"value1"} | |
| jsonString = {"key1":"value1","key2":"value2"} | |
| valueForKey1 = value1 | |
| jsonString = {"key1":"value1"} | |
| jsonString = {"key1":"value1","key2":"value2"} | |
| valueForKey1 = value1 | |
| jsonString = ["item1","item2","item3","item4"] | |
| valueFromIndex1 = item2 | |
| jsonString = ["item1","item2","item3","item4"] | |
| valueFromIndex1 = item2 | |
| jsonString = ["item1","item2","item3","item4"] | |
| valueFromIndex1 = item2 | |
| jsonString = ["item1","item2","item3","item4"] | |
| valueFromIndex1 = item2 | |
| jsonString = {"key1":"value1","valuesArray":["one","two","three"]} | |
| jsonString = {"key1":"value1","key2":"value2","valuesArray":["one","two","three"],"dateOfBirth":[2014,1,14],"arrayOfDates":[[2015,2,15],[2016,3,16],[2017,4,17]]} | |
| valueForKey1 = value1 | |
| dateOfBirth = 2014-01-14 | |
| jsonString = ["one","two","three","item4"] | |
| valueFromIndex1 = two | |
| dateFromIndex0 = 2015-02-15 | |
| dateFromIndex1 = 2016-03-16 | |
| dateFromIndex2 = 2017-04-17 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment