com.hadoopz.MyDroidLib.util.json.DroidJsonHandler Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2018 jw362j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hadoopz.MyDroidLib.util.json;
import com.mycomm.IProtocol.json.JSONClass;
import com.mycomm.IProtocol.json.JSONField;
import com.mycomm.IProtocol.log.UniversalLogSupporter;
import com.mycomm.itool.SystemUtil;
import java.lang.reflect.Field;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author jw362j
*/
public class DroidJsonHandler {
public static void ReadJson(Object instance, JSONObject data) throws JSONException, IllegalArgumentException, IllegalAccessException {
ReadJson(instance, data, null);
}
public static void ReadJson(Object instance, JSONObject data, UniversalLogSupporter logProvider) throws JSONException, IllegalArgumentException, IllegalAccessException {
if (instance == null || data == null) {
throw new IllegalArgumentException("instance or data is null in DroidJsonHandler.ReadJson()");
}
Class jclzz = instance.getClass();
if (!jclzz.isAnnotationPresent(JSONClass.class)) {
return;
// throw new RuntimeException("the JSONClass Annotation is not there!class is:" + jclzz);
}
Field[] fields = jclzz.getDeclaredFields();
if (fields == null || fields.length == 0) {
logMe(logProvider, "field is null!");
return;
}
for (Field field : fields) {
field.setAccessible(true);
if (!field.isAnnotationPresent(JSONField.class)) {
logMe(logProvider, "the field " + field + " is not JSONField!");
continue;
}
JSONField jSONField = (JSONField) field.getAnnotation(JSONField.class);
logMe(logProvider, "the field " + jSONField + " is JSONField!");
String jsonKeyName = jSONField.name();
if (SystemUtil.isTxtEmpty(jsonKeyName)) {
jsonKeyName = field.getName();
}
logMe(logProvider, "the jsonKeyName " + jsonKeyName);
if (SystemUtil.isTxtEmpty(jsonKeyName)) {
logMe(logProvider, "the crazy thing is happenning! ");
continue;
}
if (!data.has(jsonKeyName)) {
logMe(logProvider, "the JSONObject does not have " + jsonKeyName + " field!");
continue;
}
Object jsonValue = data.get(jsonKeyName);
String realDataTypeInJson = jsonValue.getClass().toString();
logMe(logProvider, "the object value in JSONObject data is:" + jsonValue);
if (jsonValue == null) {
logMe(logProvider, "the object value in JSONObject data is null !");
continue;
}
String JType = field.getType().toString().toLowerCase();
if (JType.contains("byte")) {
logMe(logProvider, "the data type is byte !");
throw new RuntimeException("the JSONClass is not support bytes!");
//field.setByte(instance, "");
}
if (JType.contains("short")) {
logMe(logProvider, "the data type is short !");
if (SystemUtil.isNumberString(jsonValue.toString())) {
field.setShort(instance, Short.valueOf(jsonValue.toString()));
}
}
if (JType.contains("float")) {
logMe(logProvider, "the data type is float !");
if (SystemUtil.isNumberString(jsonValue.toString())) {
field.setFloat(instance, Float.valueOf(jsonValue.toString()));
}
}
if (JType.contains("double")) {
logMe(logProvider, "the data type is double !");
if (SystemUtil.isNumberString(jsonValue.toString())) {
field.setDouble(instance, Double.valueOf(jsonValue.toString()));
}
}
if (JType.contains("int")) {
logMe(logProvider, "the data type is int !");
if(realDataTypeInJson.contains("java.lang.Integer")){
field.setInt(instance, data.getInt(jsonKeyName));
}
}
if (JType.contains("long")) {
logMe(logProvider, "the data type is long !");
if (SystemUtil.isNumberString(jsonValue.toString())) {
field.setLong(instance, Long.valueOf(jsonValue.toString()));
}
}
if (JType.contains("boolean")) {
logMe(logProvider, "the data type is boolean !");
field.setBoolean(instance, data.getBoolean(jsonKeyName));
}
if (JType.contains("string")) {
logMe(logProvider, "the data type is string !");
field.set(instance, jsonValue);
}
if (JType.contains("char")) {
logMe(logProvider, "the data type is char !");
// field.setChar(instance, Character.);
}
if (JType.contains("date")) {
logMe(logProvider, "the data type is date !");
}
}
}
public static void ReadMap(Object instance, Map data) throws JSONException, IllegalArgumentException, IllegalAccessException {
ReadMap(instance, data, null);
}
public static void ReadMap(Object instance, Map data, UniversalLogSupporter logProvider) throws JSONException, IllegalArgumentException, IllegalAccessException {
if (instance == null || data == null) {
throw new IllegalArgumentException("instance or data is null in DroidJsonHandler.ReadJson()");
}
Class jclzz = instance.getClass();
if (!jclzz.isAnnotationPresent(JSONClass.class)) {
throw new RuntimeException("the JSONClass Annotation is not there!class is:" + jclzz);
}
Field[] fields = jclzz.getDeclaredFields();
if (fields == null || fields.length == 0) {
logMe(logProvider, "field is null!");
return;
}
for (Field field : fields) {
field.setAccessible(true);
if (!field.isAnnotationPresent(JSONField.class)) {
logMe(logProvider, "the field " + field + " is not JSONField!");
continue;
}
JSONField jSONField = (JSONField) field.getAnnotation(JSONField.class);
logMe(logProvider, "the field " + jSONField + " is JSONField!");
String jsonKeyName = jSONField.name();
if (SystemUtil.isTxtEmpty(jsonKeyName)) {
jsonKeyName = field.getName();
}
logMe(logProvider, "the jsonKeyName " + jsonKeyName);
if (SystemUtil.isTxtEmpty(jsonKeyName)) {
logMe(logProvider, "the crazy thing is happenning! ");
continue;
}
if (data.get(jsonKeyName) == null) {
logMe(logProvider, "the JSONObject does not have " + jsonKeyName + " field!");
continue;
}
String jsonValue = data.get(jsonKeyName);
logMe(logProvider, "the object value in JSONObject data is:" + jsonValue);
if (jsonValue == null) {
logMe(logProvider, "the object value in JSONObject data is null !");
continue;
}
String JType = field.getType().toString().toLowerCase();
if (JType.contains("byte")) {
logMe(logProvider, "the data type is byte !");
throw new RuntimeException("the JSONClass is not support bytes!");
//field.setByte(instance, "");
}
if (JType.contains("short")) {
logMe(logProvider, "the data type is short !");
if (SystemUtil.isNumberString(jsonValue)) {
field.setShort(instance, Short.valueOf(jsonValue));
}
}
if (JType.contains("float")) {
logMe(logProvider, "the data type is float !");
if (SystemUtil.isNumberString(jsonValue)) {
field.setFloat(instance, Float.valueOf(jsonValue));
}
}
if (JType.contains("double")) {
logMe(logProvider, "the data type is double !");
if (SystemUtil.isNumberString(jsonValue)) {
field.setDouble(instance, Double.valueOf(jsonValue));
}
}
if (JType.contains("int")) {
logMe(logProvider, "the data type is int !");
if (SystemUtil.isNumberString(jsonValue)) {
field.setInt(instance, Integer.valueOf(jsonValue));
}
}
if (JType.contains("long")) {
logMe(logProvider, "the data type is long !");
if (SystemUtil.isNumberString(jsonValue)) {
field.setLong(instance, Long.valueOf(jsonValue));
}
}
if (JType.contains("boolean")) {
logMe(logProvider, "the data type is boolean !");
boolean rs = false;
if ("1".equals(jsonValue) || "yes".equals(jsonValue) || "ok".equals(jsonValue) || "true".equals(jsonValue)) {
rs = true;
}
field.setBoolean(instance, rs);
}
if (JType.contains("string")) {
logMe(logProvider, "the data type is string !");
field.set(instance, jsonValue);
}
if (JType.contains("char")) {
logMe(logProvider, "the data type is char !");
// field.setChar(instance, Character.);
}
if (JType.contains("date")) {
logMe(logProvider, "the data type is date !");
}
}
}
public static String BuildJsonString(Object instance) throws JSONException, IllegalArgumentException, IllegalAccessException {
return BuildJsonString(instance, null);
}
public static String BuildJsonString(Object instance, UniversalLogSupporter logProvider) throws JSONException, IllegalArgumentException, IllegalAccessException {
if (instance == null) {
throw new IllegalArgumentException("instance is null in DroidJsonHandler.WriteJson()");
}
Class jclzz = instance.getClass();
if (!jclzz.isAnnotationPresent(JSONClass.class)) {
throw new RuntimeException("the JSONClass Annotation is not there!class is:" + jclzz);
}
Field[] fields = jclzz.getDeclaredFields();
if (fields == null || fields.length == 0) {
logMe(logProvider, "field is null!");
return null;
}
JSONObject jsonObject = new JSONObject();
for (Field field : fields) {
field.setAccessible(true);
if (!field.isAnnotationPresent(JSONField.class)) {
logMe(logProvider, "the field " + field + " is not JSONField!");
continue;
}
JSONField jSONField = (JSONField) field.getAnnotation(JSONField.class);
logMe(logProvider, "the field " + jSONField + " is JSONField!");
String jsonKeyName = jSONField.name();
if (SystemUtil.isTxtEmpty(jsonKeyName)) {
jsonKeyName = field.getName();
}
logMe(logProvider, "the jsonKeyName " + jsonKeyName);
if (SystemUtil.isTxtEmpty(jsonKeyName)) {
logMe(logProvider, "the crazy thing is happenning! ");
continue;
}
String JType = field.getType().toString().toLowerCase();
if (JType.contains("byte")) {
logMe(logProvider, "the data type is byte !");
throw new RuntimeException("the JSONClass is not support bytes!");
//field.setByte(instance, "");
}
if (JType.contains("short")) {
logMe(logProvider, "the data type is short !");
jsonObject.put(jsonKeyName, field.getShort(instance));
}
if (JType.contains("float")) {
logMe(logProvider, "the data type is float !");
jsonObject.put(jsonKeyName, field.getFloat(instance));
}
if (JType.contains("double")) {
logMe(logProvider, "the data type is double !");
jsonObject.put(jsonKeyName, field.getDouble(instance));
}
if (JType.contains("int")) {
logMe(logProvider, "the data type is int !");
jsonObject.put(jsonKeyName, field.getInt(instance));
}
if (JType.contains("long")) {
logMe(logProvider, "the data type is long !");
field.setLong(instance, field.getLong(jsonKeyName));
}
if (JType.contains("boolean")) {
logMe(logProvider, "the data type is boolean !");
jsonObject.put(jsonKeyName, field.getBoolean(instance));
}
if (JType.contains("string")) {
logMe(logProvider, "the data type is string !");
jsonObject.put(jsonKeyName, field.get(instance));
}
if (JType.contains("char")) {
logMe(logProvider, "the data type is char !");
jsonObject.put(jsonKeyName, field.getChar(instance));
}
if (JType.contains("date")) {
logMe(logProvider, "the data type is date !");
//jsonObject.put(jsonKeyName, field.getChar(instance));
}
}
String rs = jsonObject.toString();
logMe(logProvider, rs);
return rs;
}
private static void logMe(UniversalLogSupporter logProvider, String msg) {
if (logProvider != null) {
logProvider.d("DroidJsonHandler", msg);
}
}
}