-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* on linux unicode problems without + 10 (JsonReader#readHexChar) * call no-arg constructor if annotated with @before (needed for e.g. field init) * if long value outside javascript safe integer range write it as text * support assigning parsable json text for java number and boolean fields * support parsing json number and json boolean for java String field
- Loading branch information
Showing
7 changed files
with
235 additions
and
20 deletions.
There are no files selected for viewing
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 one.nio.serial; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Constructor calls are skipped on JSON deserialization unless annotated with this annotation. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.CONSTRUCTOR}) | ||
public @interface Before { | ||
|
||
} |
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
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
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
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
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
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,95 @@ | ||
package one.nio.serial.gen; | ||
|
||
import one.nio.serial.Before; | ||
import one.nio.serial.Json; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
|
||
public class DelegateGeneratorTest { | ||
|
||
@Test | ||
public void testSerializeLongAsString() throws Exception { | ||
// we will fallback to writing a long value as string when we cross MIN/MAX safe integer range | ||
assertFallbackToTextForLong(false, 123L); | ||
assertFallbackToTextForLong(false, -123L); | ||
assertFallbackToTextForLong(false, 0L); | ||
assertFallbackToTextForLong(false, Json.JS_MAX_SAFE_INTEGER); | ||
assertFallbackToTextForLong(true, Json.JS_MAX_SAFE_INTEGER + 1); | ||
assertFallbackToTextForLong(false, Json.JS_MIN_SAFE_INTEGER); | ||
assertFallbackToTextForLong(true, Json.JS_MIN_SAFE_INTEGER - 1); | ||
assertFallbackToTextForLong(true, Long.MAX_VALUE); | ||
assertFallbackToTextForLong(true, Long.MIN_VALUE); | ||
} | ||
|
||
private void assertFallbackToTextForLong(boolean fallbackToText, long value) throws Exception { | ||
LongTestClass obj = new LongTestClass(); | ||
obj.longField = value; | ||
String test = Json.toJson(obj); | ||
if (fallbackToText) { | ||
Assert.assertTrue(test.contains(String.format("\"longField\":\"%d\"", value))); | ||
} else { | ||
Assert.assertTrue(test.contains(String.format("\"longField\":%d", value))); | ||
} | ||
Assert.assertEquals(value, Json.fromJson(test, LongTestClass.class).longField); | ||
} | ||
|
||
|
||
private static class LongTestClass implements Serializable { | ||
public long longField; | ||
} | ||
|
||
@Test | ||
public void testBeforeAnnotationSupport() throws IOException, ClassNotFoundException { | ||
TestClass testClass = Json.fromJson("{}", TestClass.class); | ||
Assert.assertNull(testClass.name); | ||
Assert.assertFalse(testClass.called); | ||
|
||
TestClassWithBeforeAnnotation beforeClass = Json.fromJson("{}", TestClassWithBeforeAnnotation.class); | ||
Assert.assertEquals("foo", beforeClass.name); | ||
Assert.assertEquals("bar", beforeClass.name2); | ||
Assert.assertEquals(true, beforeClass.called); | ||
|
||
ValidExtendingOfClassWithBeforeAnnotation beforeExtended = Json.fromJson("{}", ValidExtendingOfClassWithBeforeAnnotation.class); | ||
Assert.assertEquals("foo", beforeExtended.name); | ||
Assert.assertEquals("bar", beforeExtended.name2); | ||
Assert.assertEquals(true, beforeExtended.called); | ||
|
||
try { | ||
Json.fromJson("{}", InvalidExtendingOfClassWithBeforeAnnotation.class); | ||
Assert.fail(); | ||
} catch (IllegalArgumentException e) { | ||
Assert.assertTrue(e.getMessage().contains(InvalidExtendingOfClassWithBeforeAnnotation.class.getName())); | ||
} | ||
} | ||
|
||
private static class InvalidExtendingOfClassWithBeforeAnnotation extends TestClassWithBeforeAnnotation { | ||
} | ||
|
||
private static class ValidExtendingOfClassWithBeforeAnnotation extends TestClassWithBeforeAnnotation { | ||
@Before | ||
public ValidExtendingOfClassWithBeforeAnnotation() { | ||
} | ||
} | ||
|
||
private static class TestClassWithBeforeAnnotation extends TestClass implements Serializable { | ||
public String name2; | ||
|
||
@Before | ||
public TestClassWithBeforeAnnotation() { | ||
name2 = "bar"; | ||
} | ||
} | ||
|
||
private static class TestClass implements Serializable { | ||
public String name; | ||
public boolean called; | ||
|
||
public TestClass() { | ||
name = "foo"; | ||
called = true; | ||
} | ||
} | ||
} |