Search in sources :

Example 1 with JSONException

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

the class FastJsonSerializationTest method test_MediaContent_WithType_badStream.

@Test
public void test_MediaContent_WithType_badStream() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(mediaContent);
    objectOutput.flushBuffer();
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    for (int i = 0; i < byteArray.length; i++) {
        if (i % 3 == 0) {
            byteArray[i] = (byte) ~byteArray[i];
        }
    }
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    try {
        ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
        // local variable, convenient for debug
        @SuppressWarnings("unused") Object read = deserialize.readObject(MediaContent.class);
        fail();
    } catch (JSONException expected) {
        System.out.println(expected);
    }
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONException(com.alibaba.fastjson.JSONException) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Test(org.junit.Test)

Example 2 with JSONException

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

the class FastJsonSerializationTest method test_MediaContent_badStream.

@Test
public void test_MediaContent_badStream() throws Exception {
    ObjectOutput objectOutput = serialization.serialize(url, byteArrayOutputStream);
    objectOutput.writeObject(mediaContent);
    objectOutput.flushBuffer();
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    for (int i = 0; i < byteArray.length; i++) {
        if (i % 3 == 0) {
            byteArray[i] = (byte) ~byteArray[i];
        }
    }
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    try {
        ObjectInput deserialize = serialization.deserialize(url, byteArrayInputStream);
        // local variable, convenient for debug
        @SuppressWarnings("unused") Object read = deserialize.readObject();
        fail();
    } catch (JSONException expected) {
        System.out.println(expected);
    }
}
Also used : ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) JSONException(com.alibaba.fastjson.JSONException) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Test(org.junit.Test)

Example 3 with JSONException

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

the class JSONLexerBase method scanString.

public String scanString(char expectNextChar) {
    matchStat = UNKNOWN;
    int offset = 0;
    char chLocal = charAt(bp + (offset++));
    if (chLocal == 'n') {
        if (charAt(bp + offset) == 'u' && charAt(bp + offset + 1) == 'l' && charAt(bp + offset + 2) == 'l') {
            offset += 3;
            chLocal = charAt(bp + (offset++));
        } else {
            matchStat = NOT_MATCH;
            return null;
        }
        if (chLocal == expectNextChar) {
            bp += offset;
            this.ch = this.charAt(bp);
            matchStat = VALUE;
            return null;
        } else {
            matchStat = NOT_MATCH;
            return null;
        }
    }
    if (chLocal != '"') {
        matchStat = NOT_MATCH;
        return stringDefaultValue();
    }
    final String strVal;
    {
        int startIndex = bp + 1;
        int endIndex = indexOf('"', startIndex);
        if (endIndex == -1) {
            throw new JSONException("unclosed str");
        }
        String stringVal = subString(bp + 1, endIndex - startIndex);
        if (stringVal.indexOf('\\') != -1) {
            for (; ; ) {
                int slashCount = 0;
                for (int i = endIndex - 1; i >= 0; --i) {
                    if (charAt(i) == '\\') {
                        slashCount++;
                    } else {
                        break;
                    }
                }
                if (slashCount % 2 == 0) {
                    break;
                }
                endIndex = indexOf('"', endIndex + 1);
            }
            int chars_len = endIndex - startIndex;
            char[] chars = sub_chars(bp + 1, chars_len);
            stringVal = readString(chars, chars_len);
        }
        offset += (endIndex - (bp + 1) + 1);
        chLocal = charAt(bp + (offset++));
        strVal = stringVal;
    }
    if (chLocal == expectNextChar) {
        bp += offset;
        this.ch = charAt(bp);
        matchStat = VALUE;
        return strVal;
    } else {
        matchStat = NOT_MATCH;
        return strVal;
    }
}
Also used : JSONException(com.alibaba.fastjson.JSONException)

Example 4 with JSONException

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

the class JSONLexerBase method scanFieldString.

public String scanFieldString(char[] fieldName) {
    matchStat = UNKNOWN;
    if (!charArrayCompare(fieldName)) {
        matchStat = NOT_MATCH_NAME;
        return stringDefaultValue();
    }
    // int index = bp + fieldName.length;
    int offset = fieldName.length;
    char chLocal = charAt(bp + (offset++));
    if (chLocal != '"') {
        matchStat = NOT_MATCH;
        return stringDefaultValue();
    }
    final String strVal;
    {
        int startIndex = bp + fieldName.length + 1;
        int endIndex = indexOf('"', startIndex);
        if (endIndex == -1) {
            throw new JSONException("unclosed str");
        }
        // must re compute
        int startIndex2 = bp + fieldName.length + 1;
        String stringVal = subString(startIndex2, endIndex - startIndex2);
        if (stringVal.indexOf('\\') != -1) {
            for (; ; ) {
                int slashCount = 0;
                for (int i = endIndex - 1; i >= 0; --i) {
                    if (charAt(i) == '\\') {
                        slashCount++;
                    } else {
                        break;
                    }
                }
                if (slashCount % 2 == 0) {
                    break;
                }
                endIndex = indexOf('"', endIndex + 1);
            }
            int chars_len = endIndex - (bp + fieldName.length + 1);
            char[] chars = sub_chars(bp + fieldName.length + 1, chars_len);
            stringVal = readString(chars, chars_len);
        }
        offset += (endIndex - (bp + fieldName.length + 1) + 1);
        chLocal = charAt(bp + (offset++));
        strVal = stringVal;
    }
    if (chLocal == ',') {
        bp += offset;
        this.ch = this.charAt(bp);
        matchStat = VALUE;
        return strVal;
    }
    if (chLocal == '}') {
        chLocal = charAt(bp + (offset++));
        if (chLocal == ',') {
            token = JSONToken.COMMA;
            bp += offset;
            this.ch = this.charAt(bp);
        } else if (chLocal == ']') {
            token = JSONToken.RBRACKET;
            bp += offset;
            this.ch = this.charAt(bp);
        } else if (chLocal == '}') {
            token = JSONToken.RBRACE;
            bp += offset;
            this.ch = this.charAt(bp);
        } else if (chLocal == EOI) {
            token = JSONToken.EOF;
            bp += (offset - 1);
            ch = EOI;
        } else {
            matchStat = NOT_MATCH;
            return stringDefaultValue();
        }
        matchStat = END;
    } else {
        matchStat = NOT_MATCH;
        return stringDefaultValue();
    }
    return strVal;
}
Also used : JSONException(com.alibaba.fastjson.JSONException)

Example 5 with JSONException

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

the class JSONReaderScanner method charAt.

public final char charAt(int index) {
    if (index >= bufLength) {
        if (bufLength == -1) {
            if (index < sp) {
                return buf[index];
            }
            return EOI;
        }
        if (bp == 0) {
            char[] buf = new char[(this.buf.length * 3) / 2];
            System.arraycopy(this.buf, bp, buf, 0, bufLength);
            int rest = buf.length - bufLength;
            try {
                int len = reader.read(buf, bufLength, rest);
                bufLength += len;
                this.buf = buf;
            } catch (IOException e) {
                throw new JSONException(e.getMessage(), e);
            }
        } else {
            int rest = bufLength - bp;
            if (rest > 0) {
                System.arraycopy(buf, bp, buf, 0, rest);
            }
            try {
                bufLength = reader.read(buf, rest, buf.length - rest);
            } catch (IOException e) {
                throw new JSONException(e.getMessage(), e);
            }
            if (bufLength == 0) {
                throw new JSONException("illegal state, textLength is zero");
            }
            if (bufLength == -1) {
                return EOI;
            }
            bufLength += rest;
            index -= bp;
            np -= bp;
            bp = 0;
        }
    }
    return buf[index];
}
Also used : JSONException(com.alibaba.fastjson.JSONException) IOException(java.io.IOException)

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