forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...ava/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/fromJsonPrimitiveTypes.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.baeldung.gson_primitive_types.all_primitive_values; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBundle; | ||
import com.google.gson.Gson; | ||
|
||
public class fromJsonPrimitiveTypes { | ||
public static void main(String[] args) { | ||
String json = "{\"value\": 17, \"shortValue\": 3, \"intValue\": 3, " | ||
+ "\"longValue\": 3, \"floatValue\": 3.5" + ", \"doubleValue\": 3.5" | ||
+ ", \"booleanValue\": true, \"charValue\": \"a\"}"; | ||
|
||
Gson gson = new Gson(); | ||
GsonBundle gsonBundle = gson.fromJson(json, GsonBundle.class); | ||
|
||
System.out.println(gsonBundle); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../java/org/baeldung/gson/gsonprimitivetypes/all_primitive_values/toJsonPrimitiveTypes.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.baeldung.gson_primitive_types.all_primitive_values; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBundle; | ||
import com.google.gson.Gson; | ||
|
||
public class toJsonPrimitiveTypes { | ||
public static void main(String[] args) { | ||
GsonBundle gsonBundle = new GsonBundle(); | ||
Gson gson = new Gson(); | ||
|
||
System.out.println(gson.toJson(gsonBundle)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...baeldung/gson/gsonprimitivetypes/boolean_value/JsonIntegerRepresentationBooleanValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.baeldung.gson_primitive_types.boolean_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBoolean; | ||
import com.baeldung.gson_primitive_types.models.GsonFloat; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonIntegerRepresentationBooleanValue { | ||
public static void main(String[] args) { | ||
// Raises exception. | ||
String json = "{\"value\": 1}"; | ||
Gson gson = new Gson(); | ||
|
||
GsonBoolean model = gson.fromJson(json, GsonBoolean.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ldung/gson/gsonprimitivetypes/boolean_value/JsonInvalidValueWithinStringBooleanValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.gson_primitive_types.boolean_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBoolean; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonInvalidValueWithinStringBooleanValue { | ||
public static void main(String[] args) { | ||
// It is ignored. | ||
String json = "{\"value\": \"15x\"}"; | ||
Gson gson = new Gson(); | ||
|
||
GsonBoolean model = gson.fromJson(json, GsonBoolean.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...org/baeldung/gson/gsonprimitivetypes/boolean_value/JsonYesRepresentationBooleanValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.gson_primitive_types.boolean_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBoolean; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonYesRepresentationBooleanValue { | ||
public static void main(String[] args) { | ||
// It fails silently. | ||
String json = "{\"value\": yes}"; | ||
Gson gson = new Gson(); | ||
|
||
GsonBoolean model = gson.fromJson(json, GsonBoolean.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/ToJsonBitString.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.baeldung.gson_primitive_types.byte_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.google.gson.*; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class ToJsonBitString { | ||
public static void main(String[] args) { | ||
GsonBuilder builder = new GsonBuilder(); | ||
builder.registerTypeAdapter(GsonBitString.class, new GsonBitStringSerializer()); | ||
|
||
Gson gson = builder.create(); | ||
GsonBitString model = new GsonBitString(); | ||
model.value = (byte) 0b1111; | ||
|
||
System.out.println(gson.toJson(model)); | ||
} | ||
|
||
static class GsonBitStringSerializer implements JsonSerializer<GsonBitString> { | ||
@Override public JsonElement serialize(GsonBitString gsonBundle, Type type, JsonSerializationContext jsonSerializationContext) { | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.addProperty("value", Integer.toBinaryString(gsonBundle.value)); | ||
return jsonObject; | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/byte_value/fromJsonBitString.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.baeldung.gson_primitive_types.byte_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.google.gson.*; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class fromJsonBitString { | ||
public static void main(String[] args) { | ||
String json = "{\"value\": \"1111\"}"; | ||
GsonBuilder gsonBuilder = new GsonBuilder(); | ||
gsonBuilder.registerTypeAdapter(GsonBitString.class, new GsonBitStringDeserializer()); | ||
|
||
Gson gson = gsonBuilder.create(); | ||
|
||
System.out.println(gson.fromJson(json, GsonBitString.class)); | ||
} | ||
|
||
static class GsonBitStringDeserializer implements JsonDeserializer<GsonBitString> { | ||
@Override public GsonBitString deserialize(JsonElement jsonElement, | ||
Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
GsonBitString gsonBitString = new GsonBitString(); | ||
gsonBitString.value = (byte) Integer.parseInt( | ||
jsonElement.getAsJsonObject().getAsJsonPrimitive("value").getAsString(), 2); | ||
return gsonBitString; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/char_value/JsonUnicodeCharValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.baeldung.gson_primitive_types.char_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.baeldung.gson_primitive_types.models.GsonLatinChar; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonUnicodeCharValue { | ||
public static void main(String[] args) { | ||
// The field is converted. | ||
Gson gson = new Gson(); | ||
String json = "{\"value\": \"\\u00AE\"}"; | ||
GsonLatinChar model = gson.fromJson(json, GsonLatinChar.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBitString.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.baeldung.gson_primitive_types.models; | ||
|
||
public class GsonBitString { | ||
public byte value = (byte) 1; | ||
|
||
public String toString() { | ||
return "{byte: " + value + "}"; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBoolean.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.baeldung.gson_primitive_types.models; | ||
|
||
public class GsonBoolean { | ||
public boolean value; | ||
|
||
public String toString() { | ||
return "{boolean: " + value + "}"; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonBundle.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.baeldung.gson_primitive_types.models; | ||
|
||
public class GsonBundle { | ||
public byte byteValue = (byte) 0x00001111; | ||
public short shortValue = (short) 3; | ||
public int intValue = 3; | ||
public long longValue = 3; | ||
public float floatValue = 3.5f; | ||
public double doubleValue = 3.5; | ||
public boolean booleanValue = true; | ||
public char charValue = 'a'; | ||
|
||
public String toString() { | ||
return "{" + "byte: " + byteValue + ", " + "short: " + shortValue + ", " | ||
+ "int: " + intValue + ", " + "long: " + longValue + ", " + "float: " | ||
+ floatValue + ", " + "double: " + doubleValue + ", " + "boolean: " | ||
+ booleanValue + ", " + "char: " + charValue + "}"; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonFloat.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.baeldung.gson_primitive_types.models; | ||
|
||
public class GsonFloat { | ||
public float value; | ||
|
||
public String toString() { | ||
return "{float: " + value + "}"; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/models/GsonLatinChar.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.baeldung.gson_primitive_types.models; | ||
|
||
public class GsonLatinChar { | ||
public char value; | ||
|
||
public String toString() { | ||
return "{char: " + value + "}"; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...a/org/baeldung/gson/gsonprimitivetypes/number_value/JsonNonCompatibleNumberTypeValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.gson_primitive_types.number_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonNonCompatibleNumberTypeValue { | ||
public static void main(String[] args) { | ||
// Raises an exception. | ||
Gson gson = new Gson(); | ||
String json = "{\"value\": 2.3}"; | ||
GsonBitString model = gson.fromJson(json, GsonBitString.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonOverflowValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.gson_primitive_types.number_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonOverflowValue { | ||
public static void main(String[] args) { | ||
// Overflow happens unnoticed. | ||
Gson gson = new Gson(); | ||
String json = "{\"value\": \"300\"}"; | ||
GsonBitString model = gson.fromJson(json, GsonBitString.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...n/java/org/baeldung/gson/gsonprimitivetypes/number_value/JsonPrecissionMismatchValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.gson_primitive_types.number_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonFloat; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonPrecissionMismatchValue { | ||
public static void main(String[] args) { | ||
String json = "{\"value\": 12.123456789123456}"; | ||
Gson gson = new Gson(); | ||
|
||
GsonFloat model = gson.fromJson(json, GsonFloat.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonEmptyValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.gson_primitive_types.special_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonEmptyValue { | ||
public static void main(String[] args) { | ||
// Raises an exception. | ||
Gson gson = new Gson(); | ||
String json = "{\"value\": \"\"}"; | ||
GsonBitString model = gson.fromJson(json, GsonBitString.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidStringValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.gson_primitive_types.special_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonInvalidStringValue { | ||
public static void main(String[] args) { | ||
// Raises an exception. | ||
Gson gson = new Gson(); | ||
String json = "{\"value\": \"15x\"}"; | ||
GsonBitString model = gson.fromJson(json, GsonBitString.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonInvalidValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.gson_primitive_types.special_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonInvalidValue { | ||
public static void main(String[] args) { | ||
// Raises an exception. | ||
Gson gson = new Gson(); | ||
String json = "{\"value\": s15s}"; | ||
GsonBitString model = gson.fromJson(json, GsonBitString.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
gson/src/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonNullValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.gson_primitive_types.special_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonNullValue { | ||
public static void main(String[] args) { | ||
// The field will just be ignored. | ||
Gson gson = new Gson(); | ||
String json = "{\"value\": null}"; | ||
GsonBitString model = gson.fromJson(json, GsonBitString.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...rc/main/java/org/baeldung/gson/gsonprimitivetypes/special_value/JsonValidStringValue.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.baeldung.gson_primitive_types.special_value; | ||
|
||
import com.baeldung.gson_primitive_types.models.GsonBitString; | ||
import com.google.gson.Gson; | ||
|
||
public class JsonValidStringValue { | ||
public static void main(String[] args) { | ||
// The field is converted. | ||
Gson gson = new Gson(); | ||
String json = "{\"value\": \"15\"}"; | ||
GsonBitString model = gson.fromJson(json, GsonBitString.class); | ||
|
||
System.out.println(model); | ||
} | ||
} |