A web service receives a search request as JSON. In Java, Gson turns that string into a generic map, avoiding a custom request class when the payload is data-shaped. section 11.3
Gson supports two complementary edges of a data flow:
- Parse JSON text into a Java Map with gson.fromJson(jsonString, Map.class). section 11.3
- Serialize map values according to their runtime types: numbers remain unquoted, while strings receive quotes. section 10.1
Parsing a search request
Given {"title":"habit","fields":["title","weight","number_of_pages"]}, Gson produces a generic map that a service can inspect before handling the search. section 11.3
Listing 11.3 Parsing a JSON string in Java with Gson
var jsonString =
'{"title":"habit","fields":["title","weight","number_of_pages"]}';
gson.fromJson(jsonString, Map.class);
Shows the Java Gson parsing fragment for converting a JSON string into a map. section 11.3
