Tag Archives: code 10)): ha

[Solved] com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): ha

19.04.2017 14:16:10 *INFO * [catalina-exec-640072] MediaWebServer: MediaWebServer.doPost requestJson:[ {“MsgHead”:{“version”:”1.0″,”direction”:”request”,”msgType”:”POST_VOD_INFO”},”MsgBody”:{“userLan”:”Zh”,”cuType”:”PC”,”cuVersion”:”v1.0″,”cuVersionDesc”:”thirdPlat”,”systemVersion”:”windows”,”thirdPlatKey”:”6d61a6db-9cdd-4a33-8c64-7bd6449e3d4f”,”token”:”82e7ee30-c769-4395-b94a-17d75fa5ce02″,”vodName”:”1Internet Finance Report: “Financial innovation” and “standardized development” have become the two keywords of China’s Internet finance”, “vodDesc”: “Boao Forum for Asia Annual Conference 2017 Press Conference and Academic Conference of Boao Forum for Asia . Hu Bin, deputy director of the Institute of Finance of the Chinese Academy of Social Sciences, delivered a speech.
Reporter: Xinhua News Agency Boao Forum for Asia Forward Reporting Team Editor: Cao Yu “}}] (MediaWebServer.java, line 69)
19.04.2017 14:16:10 *ERROR* [catalina-exec-640072] MediaWebServer: HttpServer IOException (MediaWebServer.java, line 155)
com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value
at [Source: {“MsgHead”:{“version”:”1.0″,”direction”:”request”,”msgType”:”POST_VOD_INFO”},”MsgBody”:{“userLan”:”Zh”,”cuType”:”PC”,”cuVersion”:”v1.0″,”cuVersionDesc”:”thirdPlat”,”systemVersion”:”windows”,”thirdPlatKey”:”6d61a6db-9cdd-4a33-8c64-7bd6449e3d4f”,”token”:”82e7ee30-c769-4395-b94a-17d75fa5ce02″,”vodName”:”1Internet Finance Report: “Financial innovation” and “standardized development” have become the two keywords of China’s Internet finance”, “vodDesc”: “Boao Forum for Asia Annual Conference 2017 Press Conference and Academic Conference of Boao Forum for Asia . Hu Bin, deputy director of the Institute of Finance of the Chinese Academy of Social Sciences, delivered a speech.
Reporter: Xinhua News Agency Boao Forum for Asia Forward Reporting Team Editor: Cao Yu “}}; line: 1, column: 411]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)
at com.fasterxml.jackson.core.base.ParserMinimalBase._throwUnquotedSpace(ParserMinimalBase.java:472)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._finishString2(ReaderBasedJsonParser.java:1613)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._finishString(ReaderBasedJsonParser.java:1585)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.getText(ReaderBasedJsonParser.java:233)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:453)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.mapObject(UntypedObjectDeserializer.java:586)
at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserialize(UntypedObjectDeserializer.java:435)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:449)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:311)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2146)

Solution: add to the ObjectMapper configuration.

mapper.configure(FEATURE.ALLOW_UNQUOTED_CONTROL_CHARS,true);

More powerful, and other.

// Allow integer leads of the form 0,eg: "01"
            mapper.configure(Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
            mapper.configure(Feature.ALLOW_COMMENTS, true);
            mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
            mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
            mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);

(2) Of

jsonparseutil

package com.allcam.ryb.ads.core.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * <One sentence function brief> <Function detailed description>
 * 
 * @author Hua
 * @version [version number, 2015-01-18]
 * @see [related classes/methods]
 * @since [product/module version]
 */
public class JsonParseUtil
{
    public static final Log LOG = LogFactory.getLog(JsonParseUtil.class);
    
    private static ObjectMapper mapper = new ObjectMapper();
    
    static
    {
        // Allow integer leading to 0,eg: "01" form
        mapper.configure(Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
        mapper.configure(Feature.ALLOW_COMMENTS, true);
        mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
        mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    }
    
    /**
     * 
     * @param object
     * @return
     */
    public static String obj2Json(Object object)
    {
        try
        {
            if (null == object)
            {
                return "";
            }
            // If object is an empty string and is not processed, two double quotes will be returned
            if (object instanceof String && StringUtils.isBlank(object.toString()))
            {
                return "";
            }
            return mapper.writeValueAsString(object);
        }
        catch (Exception e)
        {
            LOG.error("Exception:Object Convert Json String Error...", e);
        }
        return "";
    }
    
    /**
     * Convert json strings to any complex objects, i.e. objects with various complex types: List, Map, Set, Object, Object[].
     * 
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T json2Obj(String json, Class<T> clazz)
    {
        try
        {
            // Set time resolution format
            mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return mapper.readValue(json, clazz);
        }
        catch (Exception e)
        {
            LOG.error("Exception:Json Convert Object Error...", e);
        }
        return null;
    }
    
    /**
     * Convert map to entity object
     * 
     * @param map
     * @param clazz
     * @return
     */
    public static <T> T map2Obj(Map<String, Object> map, Class<T> clazz)
    {
        return mapper.convertValue(map, clazz);
    }
    
    /**
     * Convert json string to copy type i.e. object containing object or List<T> case
     * 
     * @return copy type
     */
    public static <T> T json2ComplexObj(String json, Class<T> clazz)
    {
        T t = null;
        try
        {
            /**
             * Convert to complex types with TypeReference
             */
            t = mapper.readValue(json, new TypeReference<T>()
            {
            });
        }
        catch (Exception e)
        {
            LOG.error("Json Convert Complex Object Error...", e);
        }
        return t;
    }
    
    /**
     * This method is suitable for complex types of json strings ,eg:{"id":0, "userId":null} If a simple array in the shape of ["11", "22"] can be directly converted to List
     * 
     * @param jsonArr json array
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> jsonArr2List(String jsonArr, Class<T> clazz)
    {
        List<T> list = new ArrayList<T>();
        try
        {
            List<Map<String, Object>> map = mapper.readValue(jsonArr, List.class);
            for (Map<String, Object> entry : map)
            {
                T t = map2Obj(entry, clazz);
                list.add(t);
            }
        }
        catch (Exception e)
        {
            LOG.error("Json Convert List Collection Error...", e);
        }
        return list;
    }
    
    /**
     * Suitable for simple json arrays to List eg:List<String>,List<Integer>
     * 
     * @param jsonArr ["111","222"],[1, 2]
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> simpleJson2List(String jsonArr, Class<T> clazz)
    {
        List<T> list = new ArrayList<T>();
        try
        {
            list = mapper.readValue(jsonArr, List.class);
        }
        catch (Exception e)
        {
            LOG.error("Json Convert List Collection Error...", e);
        }
        return list;
    }
    
    /**
     * Convert json string to Map
     * 
     * @param json
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> json2Map(String json)
    {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        try
        {
            map = mapper.readValue(json, Map.class);
        }
        catch (Exception e)
        {
            LOG.error("Json Convert Map Error...", e);
        }
        return map;
    }
    
}