diff --git a/UserGuide.md b/UserGuide.md
index 3f13594e48..3764b828a9 100644
--- a/UserGuide.md
+++ b/UserGuide.md
@@ -14,6 +14,7 @@
* [Array Examples](#TOC-Array-Examples)
* [Collections Examples](#TOC-Collections-Examples)
* [Collections Limitations](#TOC-Collections-Limitations)
+ * [Maps Examples](#TOC-Maps-Examples)
* [Serializing and Deserializing Generic Types](#TOC-Serializing-and-Deserializing-Generic-Types)
* [Serializing and Deserializing Collection with Objects of Arbitrary Types](#TOC-Serializing-and-Deserializing-Collection-with-Objects-of-Arbitrary-Types)
* [Built-in Serializers and Deserializers](#TOC-Built-in-Serializers-and-Deserializers)
@@ -69,20 +70,23 @@ Gson was originally created for use inside Google where it is currently used in
The primary class to use is [`Gson`](gson/src/main/java/com/google/gson/Gson.java) which you can just create by calling `new Gson()`. There is also a class [`GsonBuilder`](gson/src/main/java/com/google/gson/GsonBuilder.java) available that can be used to create a Gson instance with various settings like version control and so on.
-The Gson instance does not maintain any state while invoking Json operations. So, you are free to reuse the same object for multiple Json serialization and deserialization operations.
+The Gson instance does not maintain any state while invoking JSON operations. So, you are free to reuse the same object for multiple JSON serialization and deserialization operations.
## Using Gson with Gradle/Android
-```
+
+```gradle
dependencies {
implementation 'com.google.code.gson:gson:2.9.0'
}
```
+
## Using Gson with Maven
+
To use Gson with Maven2/3, you can use the Gson version available in Maven Central by adding the following dependency:
```xml
-
+
com.google.code.gson
gson
@@ -92,7 +96,7 @@ To use Gson with Maven2/3, you can use the Gson version available in Maven Centr
```
-That is it, now your maven project is Gson enabled.
+That is it, now your Maven project is Gson enabled.
### Primitives Examples
@@ -129,7 +133,7 @@ class BagOfPrimitives {
// Serialization
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
-String json = gson.toJson(obj);
+String json = gson.toJson(obj);
// ==> json is {"value1":1,"value2":"abc"}
```
@@ -160,23 +164,23 @@ Gson can serialize static nested classes quite easily.
Gson can also deserialize static nested classes. However, Gson can **not** automatically deserialize the **pure inner classes since their no-args constructor also need a reference to the containing Object** which is not available at the time of deserialization. You can address this problem by either making the inner class static or by providing a custom InstanceCreator for it. Here is an example:
```java
-public class A {
- public String a;
+public class A {
+ public String a;
- class B {
+ class B {
- public String b;
+ public String b;
public B() {
// No args constructor for B
}
- }
+ }
}
```
**NOTE**: The above class B can not (by default) be serialized with Gson.
-Gson can not deserialize `{"b":"abc"}` into an instance of B since the class B is an inner class. If it was defined as static class B then Gson would have been able to deserialize the string. Another solution is to write a custom instance creator for B.
+Gson can not deserialize `{"b":"abc"}` into an instance of B since the class B is an inner class. If it was defined as static class B then Gson would have been able to deserialize the string. Another solution is to write a custom instance creator for B.
```java
public class InstanceCreatorForB implements InstanceCreator {
@@ -204,7 +208,7 @@ gson.toJson(ints); // ==> [1,2,3,4,5]
gson.toJson(strings); // ==> ["abc", "def", "ghi"]
// Deserialization
-int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class);
+int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class);
// ==> ints2 will be same as ints
```
@@ -214,7 +218,7 @@ We also support multi-dimensional arrays, with arbitrarily complex element types
```java
Gson gson = new Gson();
-Collection ints = Lists.immutableList(1,2,3,4,5);
+Collection ints = Arrays.asList(1,2,3,4,5);
// Serialization
String json = gson.toJson(ints); // ==> json is [1,2,3,4,5]
@@ -233,6 +237,73 @@ Unfortunately, there is no way to get around this in Java.
Gson can serialize collection of arbitrary objects but can not deserialize from it, because there is no way for the user to indicate the type of the resulting object. Instead, while deserializing, the Collection must be of a specific, generic type.
This makes sense, and is rarely a problem when following good Java coding practices.
+### Maps Examples
+
+Gson by default serializes any `java.util.Map` implementation as a JSON object. Because JSON objects only support strings as member names, Gson converts the Map keys to strings by calling `toString()` on them, and using `"null"` for `null` keys:
+
+```java
+Gson gson = new Gson();
+Map stringMap = new LinkedHashMap<>();
+stringMap.put("key", "value");
+stringMap.put(null, "null-entry");
+
+// Serialization
+String json = gson.toJson(stringMap); // ==> json is {"key":"value","null":"null-entry"}
+
+Map intMap = new LinkedHashMap<>();
+intMap.put(2, 4);
+intMap.put(3, 6);
+
+// Serialization
+String json = gson.toJson(intMap); // ==> json is {"2":4,"3":6}
+```
+
+For deserialization Gson uses the `read` method of the `TypeAdapter` registered for the Map key type. Similar to the Collection example shown above, for deserialization a `TypeToken` has to be used to tell Gson what types the Map keys and values have:
+
+```java
+Gson gson = new Gson();
+Type mapType = new TypeToken