Skip to content

Commit

Permalink
fix FastJsonHttpMessageConverter dateFormat not work, for issue #2577
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 22, 2024
1 parent 99b6399 commit 15362a0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static JSONReader.Context createReadContext(int featuresValue, Feature...
return createReadContext(JSONFactory.getDefaultObjectReaderProvider(), featuresValue, features);
}

static JSONReader.Context createReadContext(ObjectReaderProvider provider, int featuresValue, Feature... features) {
public static JSONReader.Context createReadContext(ObjectReaderProvider provider, int featuresValue, Feature... features) {
for (Feature feature : features) {
featuresValue |= feature.mask;
}
Expand Down Expand Up @@ -867,6 +867,32 @@ public static <T> T parseObject(byte[] jsonBytes, Type type, Feature... features
}
}

public static <T> T parseObject(byte[] jsonBytes, Type type, JSONReader.Context context) {
if (jsonBytes == null) {
return null;
}

JSONReader jsonReader = JSONReader.of(jsonBytes, context);

try {
ObjectReader<T> objectReader = jsonReader.getObjectReader(type);
T object = objectReader.readObject(jsonReader, null, null, 0);
if (object != null) {
jsonReader.handleResolveTasks(object);
}
if (!jsonReader.isEnd()) {
throw new JSONException(jsonReader.info("input not end"));
}
return object;
} catch (com.alibaba.fastjson2.JSONException e) {
Throwable cause = e.getCause();
if (cause == null) {
cause = e;
}
throw new JSONException(e.getMessage(), cause);
}
}

public static <T> T parseObject(byte[] jsonBytes, Type type, SerializeFilter filter, Feature... features) {
if (jsonBytes == null) {
return null;
Expand Down Expand Up @@ -2082,6 +2108,40 @@ public static final int writeJSONString(
}
}

public static final int writeJSONString(
OutputStream os,
Object object,
String dateFormat,
SerializeFilter[] filters,
SerializerFeature... features
) throws IOException {
JSONWriter.Context context = createWriteContext(SerializeConfig.global, DEFAULT_GENERATE_FEATURE, features);
if (dateFormat != null) {
context.setDateFormat(dateFormat);
}
try (JSONWriter jsonWriter = JSONWriter.ofUTF8(context)) {
for (SerializeFilter filter : filters) {
configFilter(context, filter);
}

if (object == null) {
jsonWriter.writeNull();
} else {
jsonWriter.setRootObject(object);
ObjectWriter<?> objectWriter = context.getObjectWriter(object.getClass());
objectWriter.write(jsonWriter, object, null, null, 0);
}
byte[] bytes = jsonWriter.getBytes();
os.write(bytes);
return bytes.length;
} catch (com.alibaba.fastjson2.JSONException ex) {
Throwable cause = ex.getCause() != null ? ex.getCause() : ex;
throw new JSONException("writeJSONString error", cause);
} catch (RuntimeException ex) {
throw new JSONException("writeJSONString error", ex);
}
}

public static JSONArray parseArray(String str, Feature... features) {
if (str == null || str.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONPObject;
import com.alibaba.fastjson2.JSONReader;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
Expand All @@ -21,6 +23,8 @@
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

import static com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;

/**
* Fastjson for Spring MVC Converter.
* <p>
Expand Down Expand Up @@ -123,7 +127,18 @@ private Object readType(Type type, HttpInputMessage inputMessage) {
}
byte[] bytes = baos.toByteArray();

return JSON.parseObject(bytes, type, fastJsonConfig.getFeatures());
JSONReader.Context context = JSON.createReadContext(
JSONFactory.getDefaultObjectReaderProvider(),
DEFAULT_PARSER_FEATURE,
fastJsonConfig.getFeatures()
);

String dateFormat = fastJsonConfig.getDateFormat();
if (dateFormat != null) {
context.setDateFormat(dateFormat);
}

return JSON.parseObject(bytes, type, context);
} catch (JSONException ex) {
throw new HttpMessageNotReadableException("JSON parse error: " + ex.getMessage(), ex);
} catch (IOException ex) {
Expand Down Expand Up @@ -155,6 +170,7 @@ protected void writeInternal(

contentLength = JSON.writeJSONString(
baos, object,
fastJsonConfig.getDateFormat(),
fastJsonConfig.getSerializeFilters(),
fastJsonConfig.getSerializerFeatures()
);
Expand Down

0 comments on commit 15362a0

Please sign in to comment.