Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/alibaba/fastjson2 into fix/…
Browse files Browse the repository at this point in the history
…3123
  • Loading branch information
rowstop committed Oct 26, 2024
2 parents 0b4b996 + 2b0efee commit bfb661d
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 44 deletions.
60 changes: 34 additions & 26 deletions core/src/main/java/com/alibaba/fastjson2/JSONFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,38 @@
import static com.alibaba.fastjson2.util.JDKUtils.VECTOR_BIT_LENGTH;

public final class JSONFactory {
public static final class Conf {
static final Properties DEFAULT_PROPERTIES;

static {
Properties properties = new Properties();

InputStream inputStream = AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> {
ClassLoader cl = Thread.currentThread().getContextClassLoader();

final String resourceFile = "fastjson2.properties";

if (cl != null) {
return cl.getResourceAsStream(resourceFile);
} else {
return ClassLoader.getSystemResourceAsStream(resourceFile);
}
});
if (inputStream != null) {
try {
properties.load(inputStream);
} catch (java.io.IOException ignored) {
} finally {
IOUtils.close(inputStream);
}
}
DEFAULT_PROPERTIES = properties;
}

public static String getProperty(String key) {
return DEFAULT_PROPERTIES.getProperty(key);
}
}
static volatile Throwable initErrorLast;

public static final String CREATOR;
Expand All @@ -38,7 +70,7 @@ public final class JSONFactory {
static boolean useGsonAnnotation;

public static String getProperty(String key) {
return DEFAULT_PROPERTIES.getProperty(key);
return Conf.getProperty(key);
}

static long defaultReaderFeatures;
Expand Down Expand Up @@ -136,29 +168,7 @@ public NameCacheEntry2(String name, long value0, long value1) {
static final Double DOUBLE_ZERO = (double) 0;

static {
Properties properties = new Properties();

InputStream inputStream = AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> {
ClassLoader cl = Thread.currentThread().getContextClassLoader();

final String resourceFile = "fastjson2.properties";

if (cl != null) {
return cl.getResourceAsStream(resourceFile);
} else {
return ClassLoader.getSystemResourceAsStream(resourceFile);
}
});
if (inputStream != null) {
try {
properties.load(inputStream);
} catch (java.io.IOException ignored) {
} finally {
IOUtils.close(inputStream);
}
}
DEFAULT_PROPERTIES = properties;

Properties properties = Conf.DEFAULT_PROPERTIES;
{
String property = System.getProperty("fastjson2.creator");
if (property != null) {
Expand Down Expand Up @@ -349,8 +359,6 @@ static final class CacheItem {
volatile byte[] bytes;
}

static final Properties DEFAULT_PROPERTIES;

static final ObjectWriterProvider defaultObjectWriterProvider = new ObjectWriterProvider();
static final ObjectReaderProvider defaultObjectReaderProvider = new ObjectReaderProvider();

Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,10 @@ public Map<String, Object> readObject() {
}

for (int i = 0; ; ++i) {
if (ch == '/') {
skipComment();
}

if (ch == '}') {
next();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) {
{
String property = System.getProperty(PROPERTY_DENY_PROPERTY);
if (property == null) {
property = JSONFactory.getProperty(PROPERTY_DENY_PROPERTY);
property = JSONFactory.Conf.getProperty(PROPERTY_DENY_PROPERTY);
}
if (property != null && property.length() > 0) {
DENYS = property.split(",");
Expand All @@ -69,7 +69,7 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) {
{
String property = System.getProperty(PROPERTY_AUTO_TYPE_ACCEPT);
if (property == null) {
property = JSONFactory.getProperty(PROPERTY_AUTO_TYPE_ACCEPT);
property = JSONFactory.Conf.getProperty(PROPERTY_AUTO_TYPE_ACCEPT);
}
if (property != null && property.length() > 0) {
AUTO_TYPE_ACCEPT_LIST = property.split(",");
Expand All @@ -81,7 +81,7 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) {
{
String property = System.getProperty(PROPERTY_AUTO_TYPE_BEFORE_HANDLER);
if (property == null || property.isEmpty()) {
property = JSONFactory.getProperty(PROPERTY_AUTO_TYPE_BEFORE_HANDLER);
property = JSONFactory.Conf.getProperty(PROPERTY_AUTO_TYPE_BEFORE_HANDLER);
}

if (property != null) {
Expand All @@ -104,7 +104,7 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) {
{
String property = System.getProperty(PROPERTY_AUTO_TYPE_HANDLER);
if (property == null || property.isEmpty()) {
property = JSONFactory.getProperty(PROPERTY_AUTO_TYPE_HANDLER);
property = JSONFactory.Conf.getProperty(PROPERTY_AUTO_TYPE_HANDLER);
}

if (property != null) {
Expand All @@ -127,14 +127,14 @@ public ObjectReaderCachePair(long hashCode, ObjectReader reader) {
{
String property = System.getProperty("fastjson.parser.safeMode");
if (property == null || property.isEmpty()) {
property = JSONFactory.getProperty("fastjson.parser.safeMode");
property = JSONFactory.Conf.getProperty("fastjson.parser.safeMode");
}

if (property == null || property.isEmpty()) {
property = System.getProperty("fastjson2.parser.safeMode");
}
if (property == null || property.isEmpty()) {
property = JSONFactory.getProperty("fastjson2.parser.safeMode");
property = JSONFactory.Conf.getProperty("fastjson2.parser.safeMode");
}

if (property != null) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,10 @@ public static <T> T cast(Object obj, Class<T> targetClass, ObjectReaderProvider
return (T) JdbcSupport.createTimestamp(millis);
case "java.sql.Time":
return (T) JdbcSupport.createTime(millis);
case "java.time.LocalDateTime":
return (T) LocalDateTime.ofInstant(
Instant.ofEpochMilli(millis),
DateUtils.DEFAULT_ZONE_ID);
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -28,7 +29,9 @@ public void testJsonBackReference() throws Exception {
final String jsonJackson = mapper.writeValueAsString(dto);
final String jsonFastjson2 = JSON.toJSONString(dto, JSONWriter.Feature.WriteNulls);

assertEquals(jsonJackson, jsonFastjson2);
Map<String, Object> mapJackson = mapper.readValue(jsonJackson, Map.class);
Map<String, Object> mapFastjson2 = mapper.readValue(jsonFastjson2, Map.class);
assertEquals(mapJackson, mapFastjson2);
}

static class JsonManagedReferenceDTO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
Expand Down Expand Up @@ -44,10 +45,14 @@ public void test() throws Exception {
User user = new User("test", Type.B);
String test = JSON.toJSONString(user);
String jacksonResult = objectMapper.writeValueAsString(user);
assertEquals(jacksonResult, test);
ObjectNode testNode = (ObjectNode) objectMapper.readTree(test);
ObjectNode jacksonNode = (ObjectNode) objectMapper.readTree(jacksonResult);
assertEquals(jacksonNode, testNode);
String jsonString = JSON.toJSONString(Type.A);
ObjectNode typeANode = (ObjectNode) objectMapper.readTree(jsonString);
ObjectNode expectedTypeANode = (ObjectNode) objectMapper.readTree(objectMapper.writeValueAsString(Type.A));
assertEquals(
objectMapper.writeValueAsString(Type.A),
jsonString);
typeANode,
expectedTypeANode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.alibaba.fastjson2.issues_2900;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class Issue2983 {
@Test
public void test() {
String text = "{\n" +
" \"a\": {\n" +
" \"key\": 1 // test\n" +
" }\n" +
"}";

JSONObject jsonObject = JSON.parseObject(text);
assertNotNull(jsonObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.alibaba.fastjson2.issues_3000;

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class Issue3091 {
@Test
public void test() {
String json = "{\"time\":0}";
JSONObject jsonObject = JSONObject.parseObject(json);
Time2 bean = jsonObject.toJavaObject(Time2.class);
assertNotNull(bean.time);
assertEquals(1970, bean.time.getYear());
}

@Data
public static class Time2 {
private LocalDateTime time;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue3831 {
@Test
public void test_for_issue3831() {
Map<String, Object> longType = new HashMap<>();
Map<String, Object> longType = new LinkedHashMap<>();
longType.put("type", "long");

Map<String, Object> textType = new HashMap<>();
Map<String, Object> textType = new LinkedHashMap<>();
textType.put("type", "text");
textType.put("analyzer", "standard");

Map<String, Object> raw = new HashMap<>();
Map<String, Object> raw = new LinkedHashMap<>();
raw.put("type", "keyword");
raw.put("doc_values", "false");

Map<String, Object> fields = new HashMap<>();
Map<String, Object> fields = new LinkedHashMap<>();
fields.put("raw", raw);

Map<String, Object> rawLongType = new HashMap<>();
Map<String, Object> rawLongType = new LinkedHashMap<>();
rawLongType.put("type", "long");
rawLongType.put("fields", fields);

Map<String, Object> properties = new HashMap<>();
Map<String, Object> properties = new LinkedHashMap<>();
properties.put("id", longType);
properties.put("name", textType);
properties.put("cityId", longType);
properties.put("categoryId", rawLongType);

Map<String, Object> result = new HashMap<>();
Map<String, Object> result = new LinkedHashMap<>();
result.put("properties", properties);

assertEquals(com.alibaba.fastjson2.JSON.toJSONString(result), new com.google.gson.Gson().toJson(result));
Expand Down
14 changes: 14 additions & 0 deletions docs/projects_using.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,17 @@
* https://github.com/wujun728/jun_java_plugin
* https://github.com/anhan123456/vrpc
* https://github.com/spring-projects/spring-ai
* https://github.com/wujun728/jun_java_plugin
* https://github.com/Alanosy/online-exam-system-backend
* https://github.com/zcode-zjw/zcode-commons
* https://github.com/WeBankFinTech/Exchangis
* https://github.com/Harries/springboot-demo
* https://github.com/642933588/jiron-cloud
* https://github.com/cool-team-official/cool-admin-java
* https://github.com/akto-api-security/akto
* https://github.com/Devo919/Gewechat
* https://github.com/KouShenhai/KCloud-Platform-IoT
* https://github.com/starmcc/qs-beanfun
* https://github.com/Shleter587/aippt_PresentationGen
* https://github.com/dromara/warm-flow
* https://github.com/lich0821/WeChatFerry

0 comments on commit bfb661d

Please sign in to comment.