Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
fastjson.properties support 'parser.features.ErrorOnEnumNotMatch'
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 20, 2019
1 parent 0b9f09b commit 0546d84
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 55 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.55_preview_06</version>
<version>1.2.56_preview_01</version>

<packaging>jar</packaging>
<name>fastjson</name>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/alibaba/fastjson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ private static void config(Properties properties) {
DEFAULT_PARSER_FEATURE |= Feature.NonStringKeyAsString.getMask();
}
}

{
if ("true".equals(properties.getProperty("parser.features.ErrorOnEnumNotMatch"))) {
DEFAULT_PARSER_FEATURE |= Feature.ErrorOnEnumNotMatch.getMask();
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.alibaba.json.bvt.issue_2200;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import junit.framework.TestCase;

import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ public void test_enum() throws Exception {
Assert.assertEquals("'Small'", JSON.toJSONString(Type.Small, SerializerFeature.UseSingleQuotes)); // "Small"
}

public void test_empty() throws Exception {
Model model = JSON.parseObject("{\"type\":\"\"}", Model.class);
assertNull(model.type);
}

public static class Model {
public Type type;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ public void test_0() throws Exception {
config.addFilter(ModelA.class, //
new ContextValueFilter() {

@Override
public Object process(BeanContext context, Object object, String name, Object value) {
return 30001;
}
});
config.addFilter(ModelB.class, //
new ContextValueFilter() {

@Override
public Object process(BeanContext context, Object object, String name, Object value) {
return 20001;
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/alibaba/json/bvt/util/UTF8DecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,29 @@ public void test_4() throws Exception {

}
}

/**
* @deprecated
*/
public void test_5() throws Exception {
UTF8Decoder decoder = new UTF8Decoder();

String str = "⌛︎€\uD83D\uDC69\uD83D\uDC68\uD83D\uDC68\uD83C\uDFFB\uD83D\uDC69\uD83C\uDFFFU+1F9D2: Child\tText\t\uD83E\uDDD2\t\uD83E\uDDD2\uD83C\uDFFB\t\uD83E\uDDD2\uD83C\uDFFC\t\uD83E\uDDD2\uD83C\uDFFD\t\uD83E\uDDD2\uD83C\uDFFE\t\uD83E\uDDD2\uD83C\uDFFF\n\uD83E\uDDD1\uD83C\uDFFF\uD83C\uDE1A\uD83C\uDC04️❤️";
byte[] bytes =str.getBytes("UTF-8");
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
decoder.decode(byteBuffer);
}

/**
* @deprecated
*/
public void test_6() throws Exception {
UTF8Decoder decoder = new UTF8Decoder();

String str = "\u20AC";
byte[] bytes =str.getBytes("UTF-8");
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
decoder.decode(byteBuffer);
}

}
31 changes: 31 additions & 0 deletions src/test/java/com/alibaba/json/test/A1.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.alibaba.json.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.IOUtils;
import junit.framework.TestCase;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
* Created by wenshao on 14/03/2017.
*/
Expand All @@ -13,5 +20,29 @@ public void test_a() throws Exception {
Object obj = JSON.parse("[{\"feature\":\"\\u3A56\\u3A26\"}]");
String json = JSON.toJSONString(obj, SerializerFeature.BrowserCompatible);
System.out.println(json);


}


public void test_ser() throws Exception {
// JSONObject obj = new JSONObject();
//
// ByteArrayOutputStream out = new ByteArrayOutputStream();
// ObjectOutputStream objOut = new ObjectOutputStream(out);
// objOut.writeObject(obj);
// objOut.flush();
// objOut.close();
// byte[] bytes = out.toByteArray();
//
// String str = Base64.encodeToString(bytes, false);
// System.out.println(str);

byte[] bytes2 = IOUtils.decodeBase64("rO0ABXNyAB9jb20uYWxpYmFiYS5mYXN0anNvbi5KU09OT2JqZWN0AAAAAAAAAAECAAFMAANtYXB0AA9MamF2YS91dGlsL01hcDt4cHNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAABB3CAAAABAAAAAAeA==");
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes2);
ObjectInputStream objIn = new ObjectInputStream(byteIn);
Object obj = objIn.readObject();
assertEquals(JSONObject.class, obj.getClass());

}
}
48 changes: 24 additions & 24 deletions src/test/java/com/alibaba/json/test/Base64.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,56 +86,56 @@ public class Base64
// ****************************************************************************************

/** Encodes a raw byte array into a BASE64 <code>char[]</code> representation i accordance with RFC 2045.
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
* @param bytes The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
* little faster.
* @return A BASE64 encoded array. Never <code>null</code>.
*/
public final static char[] encodeToChar(byte[] sArr, boolean lineSep)
public final static char[] encodeToChar(byte[] bytes, boolean lineSep)
{
// Check special case
int sLen = sArr != null ? sArr.length : 0;
if (sLen == 0)
int bytes_len = bytes != null ? bytes.length : 0;
if (bytes_len == 0)
return new char[0];

int eLen = (sLen / 3) * 3; // Length of even 24-bits.
int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count
int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
char[] dArr = new char[dLen];
int eLen = (bytes_len / 3) * 3; // Length of even 24-bits.
int cCnt = ((bytes_len - 1) / 3 + 1) << 2; // Returned character count
int chars_len = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
char[] chars = new char[chars_len];

// Encode even 24-bits
for (int s = 0, d = 0, cc = 0; s < eLen;) {
// Copy next three bytes into lower 24 bits of int, paying attension to sign.
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);
int i = (bytes[s++] & 0xff) << 16 | (bytes[s++] & 0xff) << 8 | (bytes[s++] & 0xff);

// Encode the int into four chars
dArr[d++] = CA[(i >>> 18) & 0x3f];
dArr[d++] = CA[(i >>> 12) & 0x3f];
dArr[d++] = CA[(i >>> 6) & 0x3f];
dArr[d++] = CA[i & 0x3f];
chars[d++] = CA[(i >>> 18) & 0x3f];
chars[d++] = CA[(i >>> 12) & 0x3f];
chars[d++] = CA[(i >>> 6) & 0x3f];
chars[d++] = CA[i & 0x3f];

// Add optional line separator
if (lineSep && ++cc == 19 && d < dLen - 2) {
dArr[d++] = '\r';
dArr[d++] = '\n';
if (lineSep && ++cc == 19 && d < chars_len - 2) {
chars[d++] = '\r';
chars[d++] = '\n';
cc = 0;
}
}

// Pad and encode last bits if source isn't even 24 bits.
int left = sLen - eLen; // 0 - 2.
int left = bytes_len - eLen; // 0 - 2.
if (left > 0) {
// Prepare the int
int i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
int i = ((bytes[eLen] & 0xff) << 10) | (left == 2 ? ((bytes[bytes_len - 1] & 0xff) << 2) : 0);

// Set last four chars
dArr[dLen - 4] = CA[i >> 12];
dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];
dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '=';
dArr[dLen - 1] = '=';
chars[chars_len - 4] = CA[i >> 12];
chars[chars_len - 3] = CA[(i >>> 6) & 0x3f];
chars[chars_len - 2] = left == 2 ? CA[i & 0x3f] : '=';
chars[chars_len - 1] = '=';
}
return dArr;
return chars;
}

/** Decodes a BASE64 encoded char array. All illegal characters will be ignored and can handle both arrays with
Expand Down Expand Up @@ -517,7 +517,7 @@ public final static byte[] decode(String str)
* @param s The source string. Length 0 will return an empty array. <code>null</code> will throw an exception.
* @return The decoded array of bytes. May be of length 0.
*/
public final static byte[] decodeFast(String s)
public final static byte[] dedecodeFast(String s)
{
// Check special case
int sLen = s.length();
Expand Down
20 changes: 11 additions & 9 deletions src/test/java/com/alibaba/json/test/UTF8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ protected void setUp() throws Exception {
public void test_encode() throws Exception {


for (int i = 0; i < 5; ++i) {
f0();
}
// for (int i = 0; i < 5; ++i) {
// f0();
// }
for (int i = 0; i < 5; ++i) {
f1();
}
for (int i = 0; i < 5; ++i) {
f2();
}
// for (int i = 0; i < 5; ++i) {
// f2();
// }
}

final static int COUNT = 1000 * 1000 * 5;

private void f0() throws Exception {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
for (int i = 0; i < COUNT; ++i) {
text.getBytes(charset);
}
long millis = System.currentTimeMillis() - start;
Expand All @@ -54,7 +56,7 @@ private void f0() throws Exception {

private void f1() throws Exception {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
for (int i = 0; i < COUNT; ++i) {
IOUtils.encodeUTF8(chars, 0, chars.length, bytes);
}
long millis = System.currentTimeMillis() - start;
Expand All @@ -63,7 +65,7 @@ private void f1() throws Exception {

private void f2() throws Exception {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000; ++i) {
for (int i = 0; i < COUNT; ++i) {
charset.newEncoder().encode(CharBuffer.wrap(chars));
}
long millis = System.currentTimeMillis() - start;
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/alibaba/json/test/UTF8Test_decode.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void test_encode() throws Exception {

for (int i = 0; i < 10; ++i) {
long start = System.currentTimeMillis();
// f0(); // 764
f1(); // 695
f0(); // 764
// f1(); // 695
// f2(); // 975
long millis = System.currentTimeMillis() - start;
System.out.println("millis : " + millis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ public static void main(String[] args) throws Exception {
// executor.getCaseList().add(new EishayDecodeBytes());
// executor.getCaseList().add(new EishayEncodeOutputStream());
// executor.getCaseList().add(new EishayEncodeToBytes());
// executor.getCaseList().add(new EishayDecode());
executor.getCaseList().add(new EishayDecode()); // 1069
//JDK8_162 1094
//JDK9_01 1214
//JDK9_04 1252
//JDK10 1088
// executor.getCaseList().add(new EishayDecodeByClassName());
// executor.getCaseList().add(new EishayTreeDecode());
executor.getCaseList().add(new EishayEncode());
// executor.getCaseList().add(new EishayEncode());
// executor.getCaseList().add(new EishayEncodeManual());
// executor.getCaseList().add(new IntArray1000Decode());
// executor.getCaseList().add(new StringArray1000Decode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class JSONPathBenchmarkTest {

public static void main(String[] args) throws Exception {
for (int i = 0; i < 5; ++i) {
f0();
f0(); // 390 353
}
for (int i = 0; i < 5; ++i) {
f1();
// f1();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class IntBenchmark {
"}";

public static void main(String[] args) throws Exception {
System.out.println(System.getProperty("java.vm.name") + " " + System.getProperty("java.runtime.version"));
// Model model = new Model();
// model.v1 = new Random().nextInt();
// model.v2 = new Random().nextInt();
Expand All @@ -30,9 +31,9 @@ public static void main(String[] args) throws Exception {


for (int i = 0; i < 10; ++i) {
// perf(); // 1798
perf(); // 1798
// perf2(); // 1877
perf3(); // 20624 2334
// perf3(); // 20624 2334
}
}

Expand Down
Loading

0 comments on commit 0546d84

Please sign in to comment.