@@ -34,6 +34,7 @@ public class GsonCompatibilityMode extends Config {
3434 private final static int SURR2_LAST = 0xDFFF ;
3535 private static final String [] REPLACEMENT_CHARS ;
3636 private static final String [] HTML_SAFE_REPLACEMENT_CHARS ;
37+
3738 static {
3839 REPLACEMENT_CHARS = new String [128 ];
3940 for (int i = 0 ; i <= 0x1f ; i ++) {
@@ -53,6 +54,7 @@ public class GsonCompatibilityMode extends Config {
5354 HTML_SAFE_REPLACEMENT_CHARS ['=' ] = "\\ u003d" ;
5455 HTML_SAFE_REPLACEMENT_CHARS ['\'' ] = "\\ u0027" ;
5556 }
57+
5658 private GsonCompatibilityMode (String configName , Builder builder ) {
5759 super (configName , builder );
5860 }
@@ -278,7 +280,7 @@ public void encode(Object obj, JsonStream stream) throws IOException {
278280 _surrogate = 0 ;
279281 // Ok, then, is the second part valid?
280282 if (c < SURR2_FIRST || c > SURR2_LAST ) {
281- throw new JsonException ("Broken surrogate pair: first char 0x" + Integer .toHexString (firstPart )+ ", second 0x" + Integer .toHexString (c )+ "; illegal combination" );
283+ throw new JsonException ("Broken surrogate pair: first char 0x" + Integer .toHexString (firstPart ) + ", second 0x" + Integer .toHexString (c ) + "; illegal combination" );
282284 }
283285 c = 0x10000 + ((firstPart - SURR1_FIRST ) << 10 ) + (c - SURR2_FIRST );
284286 if (c > 0x10FFFF ) { // illegal in JSON as well as in XML
@@ -334,6 +336,81 @@ public Object decode(JsonIterator iter) throws IOException {
334336 }
335337 }
336338 };
339+ } else if (boolean .class == type ) {
340+ return new Decoder .BooleanDecoder () {
341+ @ Override
342+ public boolean decodeBoolean (JsonIterator iter ) throws IOException {
343+ ValueType valueType = iter .whatIsNext ();
344+ if (valueType == ValueType .BOOLEAN ) {
345+ return iter .readBoolean ();
346+ } else if (valueType == ValueType .NULL ) {
347+ iter .skip ();
348+ return false ;
349+ } else {
350+ throw new JsonException ("expect boolean, but found " + valueType );
351+ }
352+ }
353+ };
354+ } else if (long .class == type ) {
355+ return new Decoder .LongDecoder () {
356+ @ Override
357+ public long decodeLong (JsonIterator iter ) throws IOException {
358+ ValueType valueType = iter .whatIsNext ();
359+ if (valueType == ValueType .NUMBER ) {
360+ return iter .readLong ();
361+ } else if (valueType == ValueType .NULL ) {
362+ iter .skip ();
363+ return 0 ;
364+ } else {
365+ throw new JsonException ("expect long, but found " + valueType );
366+ }
367+ }
368+ };
369+ } else if (int .class == type ) {
370+ return new Decoder .IntDecoder () {
371+ @ Override
372+ public int decodeInt (JsonIterator iter ) throws IOException {
373+ ValueType valueType = iter .whatIsNext ();
374+ if (valueType == ValueType .NUMBER ) {
375+ return iter .readInt ();
376+ } else if (valueType == ValueType .NULL ) {
377+ iter .skip ();
378+ return 0 ;
379+ } else {
380+ throw new JsonException ("expect int, but found " + valueType );
381+ }
382+ }
383+ };
384+ } else if (float .class == type ) {
385+ return new Decoder .FloatDecoder () {
386+ @ Override
387+ public float decodeFloat (JsonIterator iter ) throws IOException {
388+ ValueType valueType = iter .whatIsNext ();
389+ if (valueType == ValueType .NUMBER ) {
390+ return iter .readFloat ();
391+ } else if (valueType == ValueType .NULL ) {
392+ iter .skip ();
393+ return 0.0f ;
394+ } else {
395+ throw new JsonException ("expect float, but found " + valueType );
396+ }
397+ }
398+ };
399+ } else if (double .class == type ) {
400+ return new Decoder .DoubleDecoder () {
401+ @ Override
402+ public double decodeDouble (JsonIterator iter ) throws IOException {
403+ ValueType valueType = iter .whatIsNext ();
404+ if (valueType == ValueType .NUMBER ) {
405+ return iter .readDouble ();
406+ } else if (valueType == ValueType .NULL ) {
407+ iter .skip ();
408+ return 0.0d ;
409+ } else {
410+ throw new JsonException ("expect float, but found " + valueType );
411+ }
412+ }
413+ };
337414 }
338415 return super .createDecoder (cacheKey , type );
339416 }
0 commit comments