Class JsonConverter
- java.lang.Object
-
- com.supermap.services.rest.util.JsonConverter
-
public class JsonConverter extends java.lang.ObjectJSON converter.
You can convert a JSON string to a Java object, convert a normal Java object into a JSONObject object, and convert if the incoming object is of the base type.
Sample code:
JsonConverter jsonC = new JsonConverter(); Status status = Status.CLIENT_ERROR_BAD_REQUEST; Object obj = jsonC.toFormatedObject(status);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classJsonConverter.FieldInfoField type class.
-
Constructor Summary
Constructors Constructor and Description JsonConverter()The Constructor.
-
Method Summary
Methods Modifier and Type Method and Description static voidaddDecoderResolver(JsonDecoderResolver resolver)Adds anti-serializer resolver.static voidaddEncoderResolver(JsonEncoderResolver resolver)Adds serializer resolver.voidaddTypeRestrict(java.lang.Class<?> type, java.lang.String fieldName, JsonConverter.FieldInfo restrict)Adds a type definition that indicates a description of a field for a type<T> TcommonObjectDecoder(java.lang.String jsonStr, java.lang.Class<T> targetClass, DecoderSetting decoderSetting)Ordinary object conversion, the object to be decoded is not the type of List, Map, Set of the property.<T> TcommonObjectDecoder(java.lang.String jsonStr, java.lang.Class<T> targetClass, java.util.Set<java.lang.String> notDecoderedFieldSet, DecoderSetting decoderSetting)Ordinary object conversion, the object to be decoded is not the type of List, Map, Set of the property.static java.lang.ObjectgetEncapsulation(java.lang.String str, java.lang.Class cl)Encapsulates the encapsulated class according to the serialized string.static booleanisCustomEnum(java.lang.Object obj)Determines whether the type of obj is a custom enumeration, if a type of a field of his type and is their own type, and the field is static, final, then not serializedstatic booleanisEncapsulation(java.lang.Class cl)Determines whether the package class.static <T> TparseJson(java.lang.String json, java.lang.Class<T> clz)static <T> java.util.List<T>parseJsonToList(java.lang.String json, java.lang.Class<T> elementClz)protected java.lang.StringspecialCharConverter(java.lang.String copyJsonStr)Special character escaping<T> Tto(java.lang.String jsonStr, java.lang.Class<T> targetClass)Converts a JSON string to a target type.java.lang.ObjecttoArray(JSONArray jsonArray, java.lang.Class arrayElementType)Converts an array of JSON types into a Java array.java.lang.ObjecttoFormatedObject(java.lang.Object resourceObj)${services_rest_util_JsonConverter_method_toFormatedObject_Title}static java.lang.StringtoJson(java.lang.Object o)<T> java.util.List<T>toList(java.lang.String jsonStr, java.lang.Class<T> elementClass)Converts a JSON string to a List type.java.util.Map<java.lang.String,java.lang.Object>toMap(java.lang.String jsonStr, java.util.Map<java.lang.String,java.lang.Class> nameClassMapping)Converts a JSON string to a Map type.<T> TtoObject(java.lang.String jsonStr, java.lang.Class<T> targetClass)Converts a JSON string to a Object type.java.lang.ObjecttoObject(java.lang.String jsonStr, java.lang.Class targetClass, DecoderSetting decoderSetting)<T> java.util.Set<T>toSet(java.lang.String jsonStr, java.lang.Class<T> elementClass)Converts a JSON string to a Set type.
-
-
-
Method Detail
-
getEncapsulation
public static java.lang.Object getEncapsulation(java.lang.String str, java.lang.Class cl)Encapsulates the encapsulated class according to the serialized string.
- Parameters:
str- A string that has been serialized.cl- The class to be encapsulated.- Returns:
- Returns the encapsulated class.
-
isEncapsulation
public static boolean isEncapsulation(java.lang.Class cl)
Determines whether the package class.- Parameters:
cl- The class to be judged.- Returns:
- returns whether the wrapper is needed, true is required, false is not required.
-
toJson
public static java.lang.String toJson(java.lang.Object o)
-
parseJson
public static <T> T parseJson(java.lang.String json, java.lang.Class<T> clz) throws JSONException- Throws:
JSONException
-
parseJsonToList
public static <T> java.util.List<T> parseJsonToList(java.lang.String json, java.lang.Class<T> elementClz) throws JSONException- Throws:
JSONException
-
addEncoderResolver
public static void addEncoderResolver(JsonEncoderResolver resolver)
Adds serializer resolver.
- Parameters:
resolver- The solution
-
addDecoderResolver
public static void addDecoderResolver(JsonDecoderResolver resolver)
Adds anti-serializer resolver.
- Parameters:
resolver- The solution
-
isCustomEnum
public static boolean isCustomEnum(java.lang.Object obj)
Determines whether the type of obj is a custom enumeration, if a type of a field of his type and is their own type, and the field is static, final, then not serialized
-
toFormatedObject
public java.lang.Object toFormatedObject(java.lang.Object resourceObj)
${services_rest_util_JsonConverter_method_toFormatedObject_Title}
${services_rest_util_JsonConverter_method_toFormatedObject_Description}
- Parameters:
resourceObj- ${services_rest_util_JsonConverter_method_toFormatedObject_param_resourceObj}- Returns:
- ${services_rest_util_JsonConverter_method_toFormatedObject_return}
-
to
public <T> T to(java.lang.String jsonStr, java.lang.Class<T> targetClass) throws JSONExceptionConverts a JSON string to a target type.
- Parameters:
jsonStr- JSON stringtargetClass- target class.- Returns:
- returns the converted target class.
- Throws:
JSONException- Throws a JSON exception.
-
toObject
public <T> T toObject(java.lang.String jsonStr, java.lang.Class<T> targetClass) throws JSONExceptionConverts a JSON string to a Object type.
- Parameters:
jsonStr- JSON stringtargetClass- target class.- Returns:
- returns the converted Object type object.
- Throws:
JSONException- Throws a JSON exception.
-
toList
public <T> java.util.List<T> toList(java.lang.String jsonStr, java.lang.Class<T> elementClass) throws JSONExceptionConverts a JSON string to a List type.
- Parameters:
jsonStr- JSON stringelementClass- The element type of the target class.- Returns:
- Returns the object of the converted List type.
- Throws:
JSONException- Throws a JSON exception.
-
toSet
public <T> java.util.Set<T> toSet(java.lang.String jsonStr, java.lang.Class<T> elementClass) throws java.lang.IllegalArgumentException, JSONExceptionConverts a JSON string to a Set type.
- Parameters:
jsonStr- JSON stringelementClass- The element type of the target class.- Returns:
- Returns the object of the converted Set type.
- Throws:
java.lang.IllegalArgumentException- An IllegalArgumentException is thrown when the following conditions occur: 1.JSON string is empty; 2.elementClass is empty.JSONException- Throws a JSON exception.
-
toMap
public java.util.Map<java.lang.String,java.lang.Object> toMap(java.lang.String jsonStr, java.util.Map<java.lang.String,java.lang.Class> nameClassMapping)Converts a JSON string to a Map type.
- Parameters:
jsonStr- JSON stringnameClassMapping- java.util.Map mapping set. Where the key is the field name and the value is the type of the field.- Returns:
- java.util.Map parameter mapping set. Where the key is the field name and the value is the corresponding Java object.
-
toArray
public java.lang.Object toArray(JSONArray jsonArray, java.lang.Class arrayElementType) throws JSONExceptionConverts an array of JSON types into a Java array.
- Parameters:
jsonArray- The array of JSON types to convert.arrayElementType- The type of array element.- Returns:
- Java Object array.
- Throws:
JSONException- JSON exception.
-
commonObjectDecoder
public <T> T commonObjectDecoder(java.lang.String jsonStr, java.lang.Class<T> targetClass, DecoderSetting decoderSetting) throws JSONExceptionOrdinary object conversion, the object to be decoded is not the type of List, Map, Set of the property.- Throws:
JSONException
-
commonObjectDecoder
public <T> T commonObjectDecoder(java.lang.String jsonStr, java.lang.Class<T> targetClass, java.util.Set<java.lang.String> notDecoderedFieldSet, DecoderSetting decoderSetting) throws JSONExceptionOrdinary object conversion, the object to be decoded is not the type of List, Map, Set of the property.- Throws:
JSONException
-
addTypeRestrict
public void addTypeRestrict(java.lang.Class<?> type, java.lang.String fieldName, JsonConverter.FieldInfo restrict)Adds a type definition that indicates a description of a field for a type
The interface is mainly used to solve the deserialization of List, Set, Map and other types, the deserialization class does not know the type of collection elements.
- Parameters:
type- type.fieldName- field name.restrict- Added restrictions.
-
toObject
public java.lang.Object toObject(java.lang.String jsonStr, java.lang.Class targetClass, DecoderSetting decoderSetting) throws JSONException- Throws:
JSONException
-
specialCharConverter
protected java.lang.String specialCharConverter(java.lang.String copyJsonStr)
Special character escaping- Parameters:
copyJsonStr-- Returns:
-
-