File tree Expand file tree Collapse file tree
main/java/org/baeldung/gson/primitives/models
test/java/org/baeldung/gson/primitives Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package org .baeldung .gson .primitives .models ;
2+
3+ public class LongExample {
4+ public long value = 1 ;
5+
6+ public String toString () {
7+ return "{byte: " + value + "}" ;
8+ }
9+ }
Original file line number Diff line number Diff line change @@ -75,7 +75,7 @@ public class UnitTest {
7575 assertEquals (44 , model .value );
7676 }
7777
78- @ Test public void fromJsonNonCompatibleNumberTypes () {
78+ @ Test public void fromJsonRealToByte () {
7979 Gson gson = new Gson ();
8080 String json = "{\" value\" : 2.3}" ;
8181 try {
@@ -89,6 +89,27 @@ public class UnitTest {
8989 fail ();
9090 }
9191
92+ @ Test public void fromJsonRealToLong () {
93+ Gson gson = new Gson ();
94+ String json = "{\" value\" : 2.3}" ;
95+ try {
96+ gson .fromJson (json , LongExample .class );
97+ } catch (Exception ex ) {
98+ assertTrue (ex instanceof JsonSyntaxException );
99+ assertTrue (ex .getCause () instanceof NumberFormatException );
100+ return ;
101+ }
102+
103+ fail ();
104+ }
105+
106+ @ Test public void fromJsonRealToLongEndingIn0 () {
107+ Gson gson = new Gson ();
108+ String json = "{\" value\" : 2.0}" ;
109+ LongExample model = gson .fromJson (json , LongExample .class );
110+ assertEquals (2 , model .value );
111+ }
112+
92113 @ Test public void fromJsonUnicodeChar () {
93114 Gson gson = new Gson ();
94115 String json = "{\" value\" : \" \\ u00AE\" }" ;
You can’t perform that action at this time.
0 commit comments