Skip to content

Commit 5e87258

Browse files
committed
ctor support is working
1 parent 04f18cc commit 5e87258

File tree

13 files changed

+286
-253
lines changed

13 files changed

+286
-253
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.jsoniter;
2+
3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.Type;
5+
6+
public class Binding {
7+
public Class clazz;
8+
public Field field; // might be null
9+
public String[] fromNames;
10+
public String name;
11+
public Type valueType;
12+
}

src/main/java/com/jsoniter/Codegen.java

Lines changed: 186 additions & 131 deletions
Large diffs are not rendered by default.

src/main/java/com/jsoniter/CodegenAccess.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.jsoniter;
22

33
import java.io.IOException;
4-
import java.util.ArrayList;
5-
import java.util.List;
64

75
// only uesd by generated code to access decoder
86
public class CodegenAccess {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.jsoniter;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class CustomizedConstructor {
7+
/**
8+
* set to null if use constructor
9+
* otherwise use static method
10+
*/
11+
public String staticMethodName;
12+
13+
/**
14+
* the parameters to call constructor or static method
15+
*/
16+
public List<Binding> parameters = new ArrayList<Binding>();
17+
18+
public static CustomizedConstructor DEFAULT_INSTANCE = new CustomizedConstructor();
19+
}

src/main/java/com/jsoniter/EmptyExtension.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22

33
import java.lang.reflect.Field;
44
import java.lang.reflect.Type;
5+
import java.util.List;
56

67
public class EmptyExtension implements Extension {
78

89
@Override
9-
public Decoder createDecoder(Field field) {
10+
public Decoder createDecoder(Binding field) {
1011
return null;
1112
}
1213

1314
@Override
14-
public String[] getAlternativeFieldNames(Field field) {
15+
public String[] getAlternativeFieldNames(Binding field) {
1516
return null;
1617
}
1718

1819
@Override
19-
public String codegenNewInstance(Type type) {
20+
public CustomizedConstructor getConstructor(Class clazz) {
2021
return null;
2122
}
2223
}

src/main/java/com/jsoniter/Extension.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.reflect.Field;
44
import java.lang.reflect.Type;
5+
import java.util.List;
56

67
public interface Extension {
78
/**
@@ -10,21 +11,20 @@ public interface Extension {
1011
* @param field the field reflection object
1112
* @return null, if no special customization needed
1213
*/
13-
Decoder createDecoder(Field field);
14+
Decoder createDecoder(Binding field);
1415

1516
/**
1617
* Customize the field map to
1718
*
1819
* @param field the field reflection object
1920
* @return null, if fallback to default behavior
2021
*/
21-
String[] getAlternativeFieldNames(Field field);
22+
String[] getAlternativeFieldNames(Binding field);
2223

2324
/**
24-
* Generate source code for creating new instance
25-
*
26-
* @param type the type of new object to create
27-
* @return generated source code
25+
* customize which constructor to call
26+
* @param clazz the instance class to create
27+
* @return null, if fallback to default behavior
2828
*/
29-
String codegenNewInstance(Type type);
29+
CustomizedConstructor getConstructor(Class clazz);
3030
}

src/main/java/com/jsoniter/Jsoniter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,8 @@ public static void registerFieldDecoder(TypeLiteral typeLiteral, String field, D
353353
public static void registerExtension(Extension extension) {
354354
Codegen.registerExtension(extension);
355355
}
356+
357+
public static void enableStrictMode() {
358+
Codegen.enableStrictMode();
359+
}
356360
}

src/main/java/com/jsoniter/annotation/jsoniter/JsoniterAnnotationSupport.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
package com.jsoniter.annotation.jsoniter;
22

3+
import com.jsoniter.Binding;
34
import com.jsoniter.EmptyExtension;
45
import com.jsoniter.Jsoniter;
56

6-
import java.lang.reflect.Field;
7-
87
public class JsoniterAnnotationSupport extends EmptyExtension {
98

109
public static void enable() {
1110
Jsoniter.registerExtension(new JsoniterAnnotationSupport());
1211
}
1312

1413
@Override
15-
public String[] getAlternativeFieldNames(Field field) {
16-
JsonIgnore jsonIgnore = field.getAnnotation(JsonIgnore.class);
14+
public String[] getAlternativeFieldNames(Binding field) {
15+
JsonIgnore jsonIgnore = field.field.getAnnotation(JsonIgnore.class);
1716
if (jsonIgnore != null) {
1817
return new String[0];
1918
}
20-
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
19+
JsonProperty jsonProperty = field.field.getAnnotation(JsonProperty.class);
2120
if (jsonProperty != null) {
2221
String alternativeField = jsonProperty.value();
2322
if (alternativeField.equals(JsonProperty.USE_DEFAULT_NAME)) {
24-
alternativeField = field.getName();
23+
alternativeField = field.name;
2524
}
2625
return new String[]{alternativeField};
2726
}

src/test/java/com/jsoniter/CtorCustomizedObject.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.jsoniter;
2+
3+
import junit.framework.TestCase;
4+
5+
import java.io.IOException;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class TestCtor extends TestCase {
10+
11+
public static class OneArgCtor {
12+
String field1;
13+
14+
public OneArgCtor(String param1) {
15+
field1 = param1;
16+
}
17+
}
18+
19+
public void test_one_argument() throws IOException {
20+
Jsoniter.registerExtension(new EmptyExtension() {
21+
@Override
22+
public CustomizedConstructor getConstructor(Class clazz) {
23+
if (clazz == OneArgCtor.class) {
24+
return new CustomizedConstructor() {{
25+
parameters = (List) Arrays.asList(new Binding() {{
26+
fromNames = new String[]{"param1"};
27+
name="field1";
28+
valueType = String.class;
29+
}});
30+
}};
31+
}
32+
return null;
33+
}
34+
});
35+
Jsoniter iter = Jsoniter.parse("{'param1': 'hello'}".replace('\'', '"'));
36+
OneArgCtor obj = iter.read(OneArgCtor.class);
37+
assertEquals("hello", obj.field1);
38+
}
39+
}

0 commit comments

Comments
 (0)