public class JsonConverter
extends java.lang.Object
JSON 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);
Modifier and Type | Class and Description |
---|---|
static class |
JsonConverter.FieldInfo
Field type class.
|
Constructor and Description |
---|
JsonConverter()
The Constructor.
|
Modifier and Type | Method and Description |
---|---|
static void |
addDecoderResolver(JsonDecoderResolver resolver)
Adds anti-serializer resolver.
|
static void |
addEncoderResolver(JsonEncoderResolver resolver)
Adds serializer resolver.
|
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
|
<T> T |
commonObjectDecoder(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> T |
commonObjectDecoder(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.Object |
getEncapsulation(java.lang.String str, java.lang.Class cl)
Encapsulates the encapsulated class according to the serialized string.
|
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
|
static boolean |
isEncapsulation(java.lang.Class cl)
Determines whether the package class.
|
static <T> T |
parseJson(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.String |
specialCharConverter(java.lang.String copyJsonStr)
Special character escaping
|
<T> T |
to(java.lang.String jsonStr, java.lang.Class<T> targetClass)
Converts a JSON string to a target type.
|
java.lang.Object |
toArray(JSONArray jsonArray, java.lang.Class arrayElementType)
Converts an array of JSON types into a Java array.
|
java.lang.Object |
toFormatedObject(java.lang.Object resourceObj)
${services_rest_util_JsonConverter_method_toFormatedObject_Title}
|
static java.lang.String |
toJson(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> T |
toObject(java.lang.String jsonStr, java.lang.Class<T> targetClass)
Converts a JSON string to a Object type.
|
java.lang.Object |
toObject(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.
|
public static java.lang.Object getEncapsulation(java.lang.String str, java.lang.Class cl)
Encapsulates the encapsulated class according to the serialized string.
str
- A string that has been serialized.cl
- The class to be encapsulated.public static boolean isEncapsulation(java.lang.Class cl)
cl
- The class to be judged.public static java.lang.String toJson(java.lang.Object o)
public static <T> T parseJson(java.lang.String json, java.lang.Class<T> clz) throws JSONException
JSONException
public static <T> java.util.List<T> parseJsonToList(java.lang.String json, java.lang.Class<T> elementClz) throws JSONException
JSONException
public static void addEncoderResolver(JsonEncoderResolver resolver)
Adds serializer resolver.
resolver
- The solutionpublic static void addDecoderResolver(JsonDecoderResolver resolver)
Adds anti-serializer resolver.
resolver
- The solutionpublic static boolean isCustomEnum(java.lang.Object obj)
public java.lang.Object toFormatedObject(java.lang.Object resourceObj)
${services_rest_util_JsonConverter_method_toFormatedObject_Title}
${services_rest_util_JsonConverter_method_toFormatedObject_Description}
resourceObj
- ${services_rest_util_JsonConverter_method_toFormatedObject_param_resourceObj}public <T> T to(java.lang.String jsonStr, java.lang.Class<T> targetClass) throws JSONException
Converts a JSON string to a target type.
jsonStr
- JSON stringtargetClass
- target class.JSONException
- Throws a JSON exception.public <T> T toObject(java.lang.String jsonStr, java.lang.Class<T> targetClass) throws JSONException
Converts a JSON string to a Object type.
jsonStr
- JSON stringtargetClass
- target class.JSONException
- Throws a JSON exception.public <T> java.util.List<T> toList(java.lang.String jsonStr, java.lang.Class<T> elementClass) throws JSONException
Converts a JSON string to a List type.
jsonStr
- JSON stringelementClass
- The element type of the target class.JSONException
- Throws a JSON exception.public <T> java.util.Set<T> toSet(java.lang.String jsonStr, java.lang.Class<T> elementClass) throws java.lang.IllegalArgumentException, JSONException
Converts a JSON string to a Set type.
jsonStr
- JSON stringelementClass
- The element type of the target class.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.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.
jsonStr
- JSON stringnameClassMapping
- java.util.Map mapping set. Where the key is the field name and the value is the type of the field.public java.lang.Object toArray(JSONArray jsonArray, java.lang.Class arrayElementType) throws JSONException
Converts an array of JSON types into a Java array.
jsonArray
- The array of JSON types to convert.arrayElementType
- The type of array element.JSONException
- JSON exception.public <T> T commonObjectDecoder(java.lang.String jsonStr, java.lang.Class<T> targetClass, DecoderSetting decoderSetting) throws JSONException
JSONException
public <T> T commonObjectDecoder(java.lang.String jsonStr, java.lang.Class<T> targetClass, java.util.Set<java.lang.String> notDecoderedFieldSet, DecoderSetting decoderSetting) throws JSONException
JSONException
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.
type
- type.fieldName
- field name.restrict
- Added restrictions.public java.lang.Object toObject(java.lang.String jsonStr, java.lang.Class targetClass, DecoderSetting decoderSetting) throws JSONException
JSONException
protected java.lang.String specialCharConverter(java.lang.String copyJsonStr)
copyJsonStr
-