Search in sources :

Example 36 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class TypeUtils method castToLong.

public static Long castToLong(Object value) {
    if (value == null) {
        return null;
    }
    if (value instanceof Number) {
        return ((Number) value).longValue();
    }
    if (value instanceof String) {
        String strVal = (String) value;
        if (//
        strVal.length() == 0 || //
        "null".equals(strVal) || "NULL".equals(strVal)) {
            return null;
        }
        if (strVal.indexOf(',') != 0) {
            strVal = strVal.replaceAll(",", "");
        }
        try {
            return Long.parseLong(strVal);
        } catch (NumberFormatException ex) {
        //
        }
        JSONScanner dateParser = new JSONScanner(strVal);
        Calendar calendar = null;
        if (dateParser.scanISO8601DateIfMatch(false)) {
            calendar = dateParser.getCalendar();
        }
        dateParser.close();
        if (calendar != null) {
            return calendar.getTimeInMillis();
        }
    }
    throw new JSONException("can not cast to long, value : " + value);
}
Also used : JSONScanner(com.alibaba.fastjson.parser.JSONScanner) JSONException(com.alibaba.fastjson.JSONException)

Example 37 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class EnumFieldTest method test_error.

public void test_error() throws Exception {
    Exception error = null;
    try {
        JSONReader read = new JSONReader(new StringReader("{\"value\":\"a\\b\"}"));
        read.readObject(Model.class);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : StringReader(java.io.StringReader) JSONException(com.alibaba.fastjson.JSONException) JSONReader(com.alibaba.fastjson.JSONReader) JSONException(com.alibaba.fastjson.JSONException)

Example 38 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class EnumFieldTest2 method test_error_nul.

public void test_error_nul() throws Exception {
    Exception error = null;
    try {
        JSONReader read = new JSONReader(new StringReader("[nul"));
        read.config(Feature.SupportArrayToBean, true);
        Model model = read.readObject(Model.class);
        read.readObject(Model.class);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : StringReader(java.io.StringReader) JSONException(com.alibaba.fastjson.JSONException) JSONReader(com.alibaba.fastjson.JSONReader) JSONException(com.alibaba.fastjson.JSONException)

Example 39 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class EnumFieldTest2 method test_error_nu.

public void test_error_nu() throws Exception {
    Exception error = null;
    try {
        JSONReader read = new JSONReader(new StringReader("[nu"));
        read.config(Feature.SupportArrayToBean, true);
        Model model = read.readObject(Model.class);
        read.readObject(Model.class);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : StringReader(java.io.StringReader) JSONException(com.alibaba.fastjson.JSONException) JSONReader(com.alibaba.fastjson.JSONReader) JSONException(com.alibaba.fastjson.JSONException)

Example 40 with JSONException

use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.

the class EnumFieldTest2_private method test_error_1.

public void test_error_1() throws Exception {
    Exception error = null;
    try {
        JSONReader read = new JSONReader(new StringReader("[null:null]"));
        read.config(Feature.SupportArrayToBean, true);
        Model model = read.readObject(Model.class);
        read.readObject(Model.class);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : StringReader(java.io.StringReader) JSONException(com.alibaba.fastjson.JSONException) JSONReader(com.alibaba.fastjson.JSONReader) JSONException(com.alibaba.fastjson.JSONException)

Aggregations

JSONException (com.alibaba.fastjson.JSONException)484 JSONReader (com.alibaba.fastjson.JSONReader)83 StringReader (java.io.StringReader)83 JSONScanner (com.alibaba.fastjson.parser.JSONScanner)37 JSONObject (com.alibaba.fastjson.JSONObject)28 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)26 Map (java.util.Map)26 JSONLexer (com.alibaba.fastjson.parser.JSONLexer)17 IOException (java.io.IOException)17 ParseException (java.text.ParseException)8 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)7 FieldInfo (com.alibaba.fastjson.util.FieldInfo)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 JSONType (com.alibaba.fastjson.annotation.JSONType)6 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)6 Gson (com.google.gson.Gson)6 AccessibleObject (java.lang.reflect.AccessibleObject)6 Type (java.lang.reflect.Type)6 JSONField (com.alibaba.fastjson.annotation.JSONField)5 Point (java.awt.Point)5