Skip to content

Commit

Permalink
android support
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 25, 2022
1 parent 291462a commit 4148e28
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.schema.JSONSchema;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand All @@ -28,4 +29,14 @@ public void readFieldValue(JSONReader jsonReader, T object) {
throw new JSONException(jsonReader.info("set " + fieldName + " error"), e);
}
}

@Override
public void accept(T object, Object value) {
Boolean booleanValue = TypeUtils.toBoolean(value);
try {
method.invoke(object, booleanValue);
} catch (Exception e) {
throw new JSONException("set " + fieldName + " error", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.schema.JSONSchema;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand All @@ -25,12 +26,10 @@ public void readFieldValue(JSONReader jsonReader, T object) {

@Override
public void accept(T object, Object value) {
if (value == null) {
value = false;
}
boolean booleanValue = TypeUtils.toBooleanValue(value);

try {
method.invoke(object, value);
method.invoke(object, booleanValue);
} catch (Exception e) {
throw new JSONException("set " + fieldName + " error", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,26 @@ public void readFieldValue(JSONReader jsonReader, T object) {

@Override
public void accept(T object, Object value) {
double doubleValue = TypeUtils.toDoubleValue(value);

if (schema != null) {
schema.assertValidate(doubleValue);
}

try {
method.invoke(object,
TypeUtils.toDoubleValue(value));
doubleValue);
} catch (Exception e) {
throw new JSONException("set " + fieldName + " error", e);
}
}

@Override
public void accept(T object, int value) {
if (schema != null) {
schema.assertValidate(value);
}

try {
method.invoke(object, (double) value);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.schema.JSONSchema;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -31,16 +32,14 @@ public void readFieldValue(JSONReader jsonReader, T object) {

@Override
public void accept(T object, Object value) {
if (value == null) {
value = 0F;
}
float floatValue = TypeUtils.toFloatValue(value);

if (schema != null) {
schema.assertValidate(value);
schema.assertValidate(floatValue);
}

try {
method.invoke(object, value);
method.invoke(object, floatValue);
} catch (Exception e) {
throw new JSONException("set " + fieldName + " error", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ public void readFieldValue(JSONReader jsonReader, T object) {

@Override
public void accept(T object, Object value) {
Long longValue = TypeUtils.toLong(value);

if (schema != null) {
schema.assertValidate(value);
schema.assertValidate(longValue);
}

try {
method.invoke(object,
TypeUtils.toLong(value));
method.invoke(object, longValue);
} catch (Exception e) {
throw new JSONException("set " + fieldName + " error", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public void accept(T object, Object value) {
schema.assertValidate(value);
}

if (fieldClass == char.class) {
char charValue;
if (value instanceof String) {
charValue = ((String) value).charAt(0);
} else if (value instanceof Character) {
charValue = (Character) value;
} else {
throw new JSONException("cast to char error");
}
value = charValue;
}

try {
method.invoke(object, value);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void test2() {
Bean2 bean = new Bean2();
ObjectReader objectReader = TestUtils.createObjectReaderLambda(Bean2.class);
FieldReader fieldReader = objectReader.getFieldReader("value");
assertThrows(UnsupportedOperationException.class, () -> fieldReader.accept(bean, "123"));
assertThrows(Exception.class, () -> fieldReader.accept(bean, "123"));
assertThrows(Exception.class, () -> fieldReader.accept(bean, 123));
assertThrows(Exception.class, () -> fieldReader.accept(bean, (short) 123));
assertThrows(Exception.class, () -> fieldReader.accept(bean, 123L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void test2() {
Bean2 bean = new Bean2();
ObjectReader objectReader = TestUtils.createObjectReaderLambda(Bean2.class);
FieldReader fieldReader = objectReader.getFieldReader("value");
assertThrows(UnsupportedOperationException.class, () -> fieldReader.accept(bean, "123"));
assertThrows(Exception.class, () -> fieldReader.accept(bean, "123"));
assertThrows(Exception.class, () -> fieldReader.accept(bean, 123));
assertThrows(Exception.class, () -> fieldReader.accept(bean, (short) 123));
assertThrows(Exception.class, () -> fieldReader.accept(bean, 123L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void test2() {
Bean2 bean = new Bean2();
ObjectReader objectReader = TestUtils.createObjectReaderLambda(Bean2.class);
FieldReader fieldReader = objectReader.getFieldReader("value");
assertThrows(UnsupportedOperationException.class, () -> fieldReader.accept(bean, "123"));
assertThrows(Exception.class, () -> fieldReader.accept(bean, "123"));
assertThrows(Exception.class, () -> fieldReader.accept(bean, 123));
assertThrows(Exception.class, () -> fieldReader.accept(bean, (short) 123));
assertThrows(Exception.class, () -> fieldReader.accept(bean, 123L));
Expand All @@ -74,6 +74,17 @@ public void test3() {
);
}

@Test
public void test3_reflect() {
ObjectReader<Bean3> objectReader = ObjectReaderCreator.INSTANCE.createObjectReader(Bean3.class);
assertEquals(
'A',
objectReader.readObject(
JSONReader.of("{\"id\":101, \"value\":\"A\"}")
).value
);
}

public static class Bean3 {
private char value;
public final int id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ public void test1() {
);
}

@Test
public void test1_reflect() {
Bean1 bean = new Bean1();
ObjectReader<Bean1> objectReader = ObjectReaderCreator.INSTANCE.createObjectReader(Bean1.class);
FieldReader fieldReader = objectReader.getFieldReader("value");
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, "123"));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123L));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123F));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123D));

assertEquals(
201D,
objectReader.readObject(
JSONReader.of("{\"value\":201}"),
0
).value
);
}

public static class Bean1 {
@JSONField(schema = "{'minimum':128}")
private double value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ public void test1() {
);
}

@Test
public void test1_refelct() {
Bean1 bean = new Bean1();
ObjectReader<Bean1> objectReader = ObjectReaderCreator.INSTANCE.createObjectReader(Bean1.class);
FieldReader fieldReader = objectReader.getFieldReader("value");
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, "123"));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123L));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123F));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123D));

assertEquals(
201F,
objectReader.readObject(
JSONReader.of("{\"value\":201}"),
0
).value
);
}

public static class Bean1 {
@JSONField(schema = "{'minimum':128}")
private float value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ public void test1() {
);
}

@Test
public void test1_reflect() {
Bean1 bean = new Bean1();
ObjectReader<Bean1> objectReader = ObjectReaderCreator.INSTANCE.createObjectReader(Bean1.class);
FieldReader fieldReader = objectReader.getFieldReader("value");
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, "123"));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123L));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123F));
assertThrows(JSONSchemaValidException.class, () -> fieldReader.accept(bean, 123D));

assertEquals(
201L,
objectReader.readObject(
JSONReader.of("{\"value\":201}"),
0
).value
);
}

public static class Bean1 {
@JSONField(schema = "{'minimum':128}")
private Long value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,32 @@ public void test() {
);
assertEquals(123L, bean.values.get(0));

assertThrows(ClassCastException.class, () -> fieldReader.accept(bean, new Object()));
assertThrows(Exception.class, () -> fieldReader.accept(bean, new Object()));

assertEquals(
101L,
objectReader.readObject(
JSONReader.of("{\"values\":[101]}"),
0
).values.get(0)
);
}

@Test
public void test_reflect() {
Bean bean = new Bean();
ObjectReader<Bean> objectReader = ObjectReaderCreator.INSTANCE.createObjectReader(Bean.class);
FieldReader fieldReader = objectReader.getFieldReader("values");
assertNotNull(fieldReader.getMethod());
assertEquals(Long.class, fieldReader.getItemType());

fieldReader.accept(
bean,
Arrays.asList(Long.valueOf(123L))
);
assertEquals(123L, bean.values.get(0));

assertThrows(Exception.class, () -> fieldReader.accept(bean, new Object()));

assertEquals(
101L,
Expand Down Expand Up @@ -62,7 +87,7 @@ public void test1() {
);
assertEquals(123L, bean.values.get(0));

assertThrows(ClassCastException.class, () -> fieldReader.accept(bean, new Object()));
assertThrows(Exception.class, () -> fieldReader.accept(bean, new Object()));

assertEquals(
102L,
Expand Down

0 comments on commit 4148e28

Please sign in to comment.