Skip to content

Commit dbd87c6

Browse files
committed
make android demo working
1 parent a4068c9 commit dbd87c6

File tree

15 files changed

+424
-8
lines changed

15 files changed

+424
-8
lines changed

android-demo/build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ dependencies {
2929
})
3030
compile 'com.android.support:appcompat-v7:27.+'
3131
compile 'com.android.support.constraint:constraint-layout:+'
32+
compile 'com.jsoniter:jsoniter:0.9.19-SNAPSHOT'
3233
testCompile 'junit:junit:4.12'
3334
}
3435

3536
buildscript {
3637
repositories {
3738
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
3839
google()
40+
mavenLocal()
3941
}
4042
dependencies {
4143
classpath 'com.android.tools.build:gradle:2.3.0' //last version Jan 2016
@@ -46,5 +48,22 @@ allprojects {
4648
repositories {
4749
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
4850
google()
51+
mavenLocal()
4952
}
53+
}
54+
55+
afterEvaluate {
56+
android.applicationVariants.all { variant ->
57+
variant.javaCompiler.dependsOn(generateSources)
58+
}
59+
}
60+
61+
task generateSources(type:JavaExec) {
62+
classpath configurations.getByName(android.sourceSets.main.compileConfigurationName)
63+
classpath project.buildDir.toString() + '/intermediates/classes/debug'
64+
main = 'com.jsoniter.static_codegen.StaticCodegen'
65+
args 'com.example.myapplication.DemoCodegenConfig'
66+
workingDir = android.sourceSets.main.java.srcDirs[0].toString()
67+
standardOutput = System.out
68+
errorOutput = System.err
5069
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.example.myapplication;
2+
3+
import com.jsoniter.JsonIterator;
4+
import com.jsoniter.output.EncodingMode;
5+
import com.jsoniter.output.JsonStream;
6+
import com.jsoniter.spi.Decoder;
7+
import com.jsoniter.spi.DecodingMode;
8+
import com.jsoniter.spi.JsoniterSpi;
9+
import com.jsoniter.spi.TypeLiteral;
10+
import com.jsoniter.static_codegen.StaticCodegenConfig;
11+
12+
import java.io.IOException;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
public class DemoCodegenConfig implements StaticCodegenConfig {
17+
18+
@Override
19+
public void setup() {
20+
// register custom decoder or extensions before codegen
21+
// so that we doing codegen, we know in which case, we need to callback
22+
JsonIterator.setMode(DecodingMode.STATIC_MODE);
23+
JsonStream.setMode(EncodingMode.STATIC_MODE);
24+
JsonStream.setIndentionStep(2);
25+
JsoniterSpi.registerPropertyDecoder(User.class, "score", new Decoder.IntDecoder() {
26+
@Override
27+
public int decodeInt(JsonIterator iter) throws IOException {
28+
return Integer.valueOf(iter.readString());
29+
}
30+
});
31+
}
32+
33+
@Override
34+
public TypeLiteral[] whatToCodegen() {
35+
return new TypeLiteral[]{
36+
// generic types, need to use this syntax
37+
new TypeLiteral<List<Integer>>() {
38+
},
39+
new TypeLiteral<List<User>>() {
40+
},
41+
new TypeLiteral<Map<String, Object>>() {
42+
},
43+
// array
44+
TypeLiteral.create(int[].class),
45+
// object
46+
TypeLiteral.create(User.class)
47+
};
48+
}
49+
}

android-demo/src/main/java/com/example/myapplication/MainActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import android.support.v7.app.AppCompatActivity;
44
import android.os.Bundle;
5+
import android.util.Log;
6+
import com.jsoniter.JsonIterator;
57

68
public class MainActivity extends AppCompatActivity {
79

810
@Override
911
protected void onCreate(Bundle savedInstanceState) {
1012
super.onCreate(savedInstanceState);
1113
setContentView(R.layout.activity_main);
14+
User user = JsonIterator.deserialize("{\"firstName\": \"tao\", \"lastName\": \"wen\", \"score\": 1024}", User.class);
15+
Log.d("jsoniter", user.firstName);
1216
}
1317
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.myapplication;
2+
3+
import com.jsoniter.annotation.JsonProperty;
4+
5+
public class User {
6+
@JsonProperty(nullable = false)
7+
public String firstName;
8+
@JsonProperty(nullable = false)
9+
public String lastName;
10+
public int score;
11+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package jsoniter_codegen.cfg1173796797.decoder.com.example.myapplication;
2+
public class User implements com.jsoniter.spi.Decoder {
3+
public static java.lang.Object decode_(com.jsoniter.JsonIterator iter) throws java.io.IOException { java.lang.Object existingObj = com.jsoniter.CodegenAccess.resetExistingObject(iter);
4+
byte nextToken = com.jsoniter.CodegenAccess.readByte(iter);
5+
if (nextToken != '{') {
6+
if (nextToken == 'n') {
7+
com.jsoniter.CodegenAccess.skipFixedBytes(iter, 3);
8+
return null;
9+
} else {
10+
nextToken = com.jsoniter.CodegenAccess.nextToken(iter);
11+
if (nextToken == 'n') {
12+
com.jsoniter.CodegenAccess.skipFixedBytes(iter, 3);
13+
return null;
14+
}
15+
} // end of if null
16+
} // end of if {
17+
nextToken = com.jsoniter.CodegenAccess.readByte(iter);
18+
if (nextToken != '"') {
19+
if (nextToken == '}') {
20+
return (existingObj == null ? new com.example.myapplication.User() : (com.example.myapplication.User)existingObj);
21+
} else {
22+
nextToken = com.jsoniter.CodegenAccess.nextToken(iter);
23+
if (nextToken == '}') {
24+
return (existingObj == null ? new com.example.myapplication.User() : (com.example.myapplication.User)existingObj);
25+
} else {
26+
com.jsoniter.CodegenAccess.unreadByte(iter);
27+
}
28+
} // end of if end
29+
} else { com.jsoniter.CodegenAccess.unreadByte(iter); }// end of if not quote
30+
java.lang.String _firstName_ = null;
31+
java.lang.String _lastName_ = null;
32+
int _score_ = 0;
33+
do {
34+
switch (com.jsoniter.CodegenAccess.readObjectFieldAsHash(iter)) {
35+
case -1078100014:
36+
_lastName_ = (java.lang.String)iter.readString();
37+
continue;
38+
case -799547430:
39+
_firstName_ = (java.lang.String)iter.readString();
40+
continue;
41+
case -768634731:
42+
_score_ = (int)com.jsoniter.CodegenAccess.readInt("score@jsoniter_codegen.cfg1173796797.decoder.com.example.myapplication.User", iter);
43+
continue;
44+
}
45+
iter.skip();
46+
} while (com.jsoniter.CodegenAccess.nextTokenIsComma(iter));
47+
com.example.myapplication.User obj = (existingObj == null ? new com.example.myapplication.User() : (com.example.myapplication.User)existingObj);
48+
obj.firstName = _firstName_;
49+
obj.lastName = _lastName_;
50+
obj.score = _score_;
51+
return obj;
52+
}public java.lang.Object decode(com.jsoniter.JsonIterator iter) throws java.io.IOException {
53+
return decode_(iter);
54+
}
55+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package jsoniter_codegen.cfg1173796797.decoder;
2+
public class int_array implements com.jsoniter.spi.Decoder {
3+
public static java.lang.Object decode_(com.jsoniter.JsonIterator iter) throws java.io.IOException { com.jsoniter.CodegenAccess.resetExistingObject(iter);
4+
byte nextToken = com.jsoniter.CodegenAccess.readByte(iter);
5+
if (nextToken != '[') {
6+
if (nextToken == 'n') {
7+
com.jsoniter.CodegenAccess.skipFixedBytes(iter, 3);
8+
com.jsoniter.CodegenAccess.resetExistingObject(iter); return null;
9+
} else {
10+
nextToken = com.jsoniter.CodegenAccess.nextToken(iter);
11+
if (nextToken == 'n') {
12+
com.jsoniter.CodegenAccess.skipFixedBytes(iter, 3);
13+
com.jsoniter.CodegenAccess.resetExistingObject(iter); return null;
14+
}
15+
}
16+
}
17+
nextToken = com.jsoniter.CodegenAccess.nextToken(iter);
18+
if (nextToken == ']') {
19+
return new int[0];
20+
}
21+
com.jsoniter.CodegenAccess.unreadByte(iter);
22+
int a1 = (int)iter.readInt();
23+
if (!com.jsoniter.CodegenAccess.nextTokenIsComma(iter)) {
24+
return new int[]{ a1 };
25+
}
26+
int a2 = (int)iter.readInt();
27+
if (!com.jsoniter.CodegenAccess.nextTokenIsComma(iter)) {
28+
return new int[]{ a1, a2 };
29+
}
30+
int a3 = (int)iter.readInt();
31+
if (!com.jsoniter.CodegenAccess.nextTokenIsComma(iter)) {
32+
return new int[]{ a1, a2, a3 };
33+
}
34+
int a4 = (int) (int)iter.readInt();
35+
if (!com.jsoniter.CodegenAccess.nextTokenIsComma(iter)) {
36+
return new int[]{ a1, a2, a3, a4 };
37+
}
38+
int a5 = (int) (int)iter.readInt();
39+
int[] arr = new int[10];
40+
arr[0] = a1;
41+
arr[1] = a2;
42+
arr[2] = a3;
43+
arr[3] = a4;
44+
arr[4] = a5;
45+
int i = 5;
46+
while (com.jsoniter.CodegenAccess.nextTokenIsComma(iter)) {
47+
if (i == arr.length) {
48+
int[] newArr = new int[arr.length * 2];
49+
System.arraycopy(arr, 0, newArr, 0, arr.length);
50+
arr = newArr;
51+
}
52+
arr[i++] = (int)iter.readInt();
53+
}
54+
int[] result = new int[i];
55+
System.arraycopy(arr, 0, result, 0, i);
56+
return result;
57+
}public java.lang.Object decode(com.jsoniter.JsonIterator iter) throws java.io.IOException {
58+
return decode_(iter);
59+
}
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package jsoniter_codegen.cfg1173796797.decoder.java.util.List_com.example.myapplication;
2+
public class User implements com.jsoniter.spi.Decoder {
3+
public static java.lang.Object decode_(com.jsoniter.JsonIterator iter) throws java.io.IOException { java.util.ArrayList col = (java.util.ArrayList)com.jsoniter.CodegenAccess.resetExistingObject(iter);
4+
if (iter.readNull()) { com.jsoniter.CodegenAccess.resetExistingObject(iter); return null; }
5+
if (!com.jsoniter.CodegenAccess.readArrayStart(iter)) {
6+
return col == null ? new java.util.ArrayList(0): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
7+
}
8+
Object a1 = (com.example.myapplication.User)jsoniter_codegen.cfg1173796797.decoder.com.example.myapplication.User.decode_(iter);
9+
if (com.jsoniter.CodegenAccess.nextToken(iter) != ',') {
10+
java.util.ArrayList obj = col == null ? new java.util.ArrayList(1): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
11+
obj.add(a1);
12+
return obj;
13+
}
14+
Object a2 = (com.example.myapplication.User)jsoniter_codegen.cfg1173796797.decoder.com.example.myapplication.User.decode_(iter);
15+
if (com.jsoniter.CodegenAccess.nextToken(iter) != ',') {
16+
java.util.ArrayList obj = col == null ? new java.util.ArrayList(2): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
17+
obj.add(a1);
18+
obj.add(a2);
19+
return obj;
20+
}
21+
Object a3 = (com.example.myapplication.User)jsoniter_codegen.cfg1173796797.decoder.com.example.myapplication.User.decode_(iter);
22+
if (com.jsoniter.CodegenAccess.nextToken(iter) != ',') {
23+
java.util.ArrayList obj = col == null ? new java.util.ArrayList(3): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
24+
obj.add(a1);
25+
obj.add(a2);
26+
obj.add(a3);
27+
return obj;
28+
}
29+
Object a4 = (com.example.myapplication.User)jsoniter_codegen.cfg1173796797.decoder.com.example.myapplication.User.decode_(iter);
30+
java.util.ArrayList obj = col == null ? new java.util.ArrayList(8): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
31+
obj.add(a1);
32+
obj.add(a2);
33+
obj.add(a3);
34+
obj.add(a4);
35+
while (com.jsoniter.CodegenAccess.nextToken(iter) == ',') {
36+
obj.add((com.example.myapplication.User)jsoniter_codegen.cfg1173796797.decoder.com.example.myapplication.User.decode_(iter));
37+
}
38+
return obj;
39+
}public java.lang.Object decode(com.jsoniter.JsonIterator iter) throws java.io.IOException {
40+
return decode_(iter);
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package jsoniter_codegen.cfg1173796797.decoder.java.util.List_java.lang;
2+
public class Integer implements com.jsoniter.spi.Decoder {
3+
public static java.lang.Object decode_(com.jsoniter.JsonIterator iter) throws java.io.IOException { java.util.ArrayList col = (java.util.ArrayList)com.jsoniter.CodegenAccess.resetExistingObject(iter);
4+
if (iter.readNull()) { com.jsoniter.CodegenAccess.resetExistingObject(iter); return null; }
5+
if (!com.jsoniter.CodegenAccess.readArrayStart(iter)) {
6+
return col == null ? new java.util.ArrayList(0): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
7+
}
8+
Object a1 = (java.lang.Integer)(iter.readNull() ? null : java.lang.Integer.valueOf(iter.readInt()));
9+
if (com.jsoniter.CodegenAccess.nextToken(iter) != ',') {
10+
java.util.ArrayList obj = col == null ? new java.util.ArrayList(1): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
11+
obj.add(a1);
12+
return obj;
13+
}
14+
Object a2 = (java.lang.Integer)(iter.readNull() ? null : java.lang.Integer.valueOf(iter.readInt()));
15+
if (com.jsoniter.CodegenAccess.nextToken(iter) != ',') {
16+
java.util.ArrayList obj = col == null ? new java.util.ArrayList(2): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
17+
obj.add(a1);
18+
obj.add(a2);
19+
return obj;
20+
}
21+
Object a3 = (java.lang.Integer)(iter.readNull() ? null : java.lang.Integer.valueOf(iter.readInt()));
22+
if (com.jsoniter.CodegenAccess.nextToken(iter) != ',') {
23+
java.util.ArrayList obj = col == null ? new java.util.ArrayList(3): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
24+
obj.add(a1);
25+
obj.add(a2);
26+
obj.add(a3);
27+
return obj;
28+
}
29+
Object a4 = (java.lang.Integer)(iter.readNull() ? null : java.lang.Integer.valueOf(iter.readInt()));
30+
java.util.ArrayList obj = col == null ? new java.util.ArrayList(8): (java.util.ArrayList)com.jsoniter.CodegenAccess.reuseCollection(col);
31+
obj.add(a1);
32+
obj.add(a2);
33+
obj.add(a3);
34+
obj.add(a4);
35+
while (com.jsoniter.CodegenAccess.nextToken(iter) == ',') {
36+
obj.add((java.lang.Integer)(iter.readNull() ? null : java.lang.Integer.valueOf(iter.readInt())));
37+
}
38+
return obj;
39+
}public java.lang.Object decode(com.jsoniter.JsonIterator iter) throws java.io.IOException {
40+
return decode_(iter);
41+
}
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package jsoniter_codegen.cfg1173796797.decoder.java.util.Map_java.lang.String_java.lang;
2+
public class Object implements com.jsoniter.spi.Decoder {
3+
public static java.lang.Object decode_(com.jsoniter.JsonIterator iter) throws java.io.IOException { java.util.HashMap map = (java.util.HashMap)com.jsoniter.CodegenAccess.resetExistingObject(iter);
4+
if (iter.readNull()) { return null; }
5+
if (map == null) { map = new java.util.HashMap(); }
6+
if (!com.jsoniter.CodegenAccess.readObjectStart(iter)) {
7+
return map;
8+
}
9+
do {
10+
java.lang.Object mapKey = com.jsoniter.CodegenAccess.readObjectFieldAsString(iter);
11+
map.put(mapKey, (java.lang.Object)iter.read());
12+
} while (com.jsoniter.CodegenAccess.nextToken(iter) == ',');
13+
return map;
14+
}public java.lang.Object decode(com.jsoniter.JsonIterator iter) throws java.io.IOException {
15+
return decode_(iter);
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package jsoniter_codegen.cfg1173796797.encoder.com.example.myapplication;
2+
public class User implements com.jsoniter.spi.Encoder {
3+
public void encode(java.lang.Object obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {
4+
if (obj == null) { stream.writeNull(); return; }
5+
encode_((com.example.myapplication.User)obj, stream);
6+
}
7+
public static void encode_(com.example.myapplication.User obj, com.jsoniter.output.JsonStream stream) throws java.io.IOException {
8+
stream.writeObjectStart();
9+
stream.writeIndention();
10+
stream.writeObjectField("firstName");
11+
stream.writeVal((java.lang.String)obj.firstName);
12+
stream.writeMore();
13+
stream.writeObjectField("lastName");
14+
stream.writeVal((java.lang.String)obj.lastName);
15+
stream.writeMore();
16+
stream.writeObjectField("score");
17+
stream.writeVal((int)obj.score);
18+
stream.writeObjectEnd();
19+
}
20+
}

0 commit comments

Comments
 (0)