diff --git a/joylive-bom/pom.xml b/joylive-bom/pom.xml index 7a5fdd05..6a3641aa 100644 --- a/joylive-bom/pom.xml +++ b/joylive-bom/pom.xml @@ -93,6 +93,11 @@ joylive-parser-jackson ${revision} + + com.jd.live + joylive-parser-fastjson2 + ${revision} + com.jd.live joylive-parser-properties diff --git a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/parser/json/JsonFormat.java b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/parser/json/JsonFormat.java index 81046d46..662af462 100644 --- a/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/parser/json/JsonFormat.java +++ b/joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/parser/json/JsonFormat.java @@ -41,11 +41,4 @@ */ String pattern() default ""; - /** - * Specifies the timezone to be used for formatting/parsing the date/time value. - * This is particularly useful for applications that operate across multiple time zones. - * - * @return The ID of the timezone, such as "UTC" or "GMT+10". - */ - String timezone() default ""; } diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/pom.xml b/joylive-implement/joylive-parser/joylive-parser-fastjson2/pom.xml index 9a9d61d6..74e35d4a 100644 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/pom.xml +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/pom.xml @@ -10,19 +10,14 @@ 2.0.53 - 2.2 - - org.yaml - snakeyaml - ${snakeyaml.version} - com.alibaba.fastjson2 fastjson2 ${fastjson2.version} + @@ -35,7 +30,6 @@ com.alibaba.fastjson2:* - org.yaml:* @@ -56,10 +50,6 @@ com.alibaba.fastjson2 com.jd.live.agent.shaded.com.alibaba.fastjson2 - - org.yaml - com.jd.live.agent.shaded.org.yaml - diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/AbstractFastjson2Parser.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/AbstractFastjson2Parser.java deleted file mode 100644 index f9b2ae0e..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/AbstractFastjson2Parser.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2; - -import com.alibaba.fastjson2.JSONFactory; -import com.jd.live.agent.core.exception.ParseException; -import com.jd.live.agent.core.parser.ConfigParser; -import com.jd.live.agent.core.parser.ObjectParser; -import com.jd.live.agent.core.parser.TypeReference; -import org.yaml.snakeyaml.Yaml; - -import java.io.*; -import java.lang.reflect.Type; -import java.nio.charset.StandardCharsets; -import java.util.Collections; -import java.util.Map; - -public abstract class AbstractFastjson2Parser implements ConfigParser, ObjectParser { - - static { - JSONFactory.getDefaultObjectWriterProvider().register(new JoyLiveWriterModule()); - JSONFactory.getDefaultObjectReaderProvider().register(new JoyLiveReaderModule()); - } - - private final Yaml yaml = new Yaml(); - - @Override - public Map parse(Reader reader) { - return Collections.emptyMap(); - } - - @Override - public T read(Reader reader, Class clazz) { - if (reader == null || clazz == null) - return null; - try { - switch (getSupportedType()) { - case YAML: - case YML: - return com.alibaba.fastjson2.JSON.parseObject( - com.alibaba.fastjson2.JSON.toJSONString(yaml.load(reader)), - clazz - ); - default: - return com.alibaba.fastjson2.JSON.parseObject(reader, clazz); - } - } catch (Exception e) { - throw new ParseException("read error. caused by " + e.getMessage(), e); - } finally { - ProxySupport.cachedKey.remove(); - ProxySupport.jsonConverterThreadLocal.remove(); - ProxySupport.timeFormatThreadLocal.remove(); - } - } - - @Override - public T read(Reader reader, TypeReference reference) { - if (reader == null || reference == null) - return null; - try { - switch (getSupportedType()) { - case YAML: - case YML: - return com.alibaba.fastjson2.JSON.parseObject( - com.alibaba.fastjson2.JSON.toJSONString(yaml.load(reader)), - reference.getType() - ); - default: - return com.alibaba.fastjson2.JSON.parseObject(reader, reference.getType()); - } - } catch (Exception e) { - throw new ParseException("read error. caused by " + e.getMessage(), e); - } finally { - ProxySupport.cachedKey.remove(); - ProxySupport.jsonConverterThreadLocal.remove(); - ProxySupport.timeFormatThreadLocal.remove(); - } - } - - @Override - public T read(Reader reader, Type type) { - if (reader == null || type == null) - return null; - try { - switch (getSupportedType()) { - case YAML: - case YML: - return com.alibaba.fastjson2.JSON.parseObject( - com.alibaba.fastjson2.JSON.toJSONString(yaml.load(reader)), - type - ); - default: - return com.alibaba.fastjson2.JSON.parseObject(reader, type); - } - } catch (Exception e) { - throw new ParseException("read error. caused by " + e.getMessage(), e); - } finally { - ProxySupport.cachedKey.remove(); - ProxySupport.jsonConverterThreadLocal.remove(); - ProxySupport.timeFormatThreadLocal.remove(); - } - } - - @Override - public void write(Writer writer, Object obj) { - if (writer == null || obj == null) - return; - try { - ByteArrayOutputStream strStream = new ByteArrayOutputStream(); - com.alibaba.fastjson2.JSON.writeTo(strStream, obj); - String str = strStream.toString(StandardCharsets.UTF_8.name()); - writer.write(str); - writer.flush(); - } catch (Exception e) { - throw new ParseException("write error. caused by " + e.getMessage(), e); - } finally { - ProxySupport.cachedKey.remove(); - ProxySupport.jsonConverterThreadLocal.remove(); - ProxySupport.timeFormatThreadLocal.remove(); - } - } - - protected String getSupportedType() { - return JSON; - } -} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Converters.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Converters.java new file mode 100644 index 00000000..e4089721 --- /dev/null +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Converters.java @@ -0,0 +1,80 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.implement.parser.fastjson2; + +import com.jd.live.agent.core.exception.ParseException; +import com.jd.live.agent.core.parser.json.JsonConverter; + +import java.lang.reflect.Constructor; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * A utility class for managing and creating JSON converters. + */ +public class Converters { + + /** + * A map of field names to their corresponding JSON converters. + */ + private static final Map> FIELD_CONVERTERS = new ConcurrentHashMap<>(); + + /** + * A map of converter class names to their corresponding JSON converters. + */ + private static final Map> CONVERTERS = new ConcurrentHashMap<>(); + + /** + * Gets or creates a JSON converter instance for the given converter class. + * + * @param clazz The converter class to get or create an instance for. + * @return The JSON converter instance, or null if the given class is null. + * @throws ParseException If an error occurs while creating the converter instance. + */ + public static JsonConverter getOrCreateConverter(Class> clazz) { + return clazz == null ? null : CONVERTERS.computeIfAbsent(clazz.getName(), key -> { + try { + Constructor> constructor = clazz.getConstructor(); + constructor.setAccessible(true); + return constructor.newInstance(); + } catch (Throwable e) { + throw new ParseException(e.getMessage(), e); + } + }); + } + + /** + * Gets or creates a JSON converter instance for the given field name and converter class. + * + * @param fieldName The field name to get or create a converter instance for. + * @param clazz The converter class to get or create an instance for. + * @return The JSON converter instance, or null if either the field name or the converter class is null. + */ + public static JsonConverter getOrCreateConverter(String fieldName, Class> clazz) { + return fieldName == null || clazz == null ? null : FIELD_CONVERTERS.computeIfAbsent(fieldName, name -> getOrCreateConverter(clazz)); + } + + /** + * Gets the JSON converter instance for the given field name. + * + * @param fieldName The field name to get the converter instance for. + * @return The JSON converter instance, or null if no converter instance exists for the given field name. + */ + public static JsonConverter getConverter(String fieldName) { + return fieldName == null ? null : FIELD_CONVERTERS.get(fieldName); + } + +} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2JsonParser.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2JsonParser.java index 17fc2fd0..1d35a9bb 100644 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2JsonParser.java +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2JsonParser.java @@ -1,30 +1,77 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.jd.live.agent.implement.parser.fastjson2; -import com.alibaba.fastjson2.JSONPath; +import com.alibaba.fastjson2.JSONFactory; +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONWriter; +import com.jd.live.agent.core.exception.ParseException; import com.jd.live.agent.core.extension.annotation.Extension; -import com.jd.live.agent.core.parser.JsonPathParser; import com.jd.live.agent.core.parser.ObjectParser; +import com.jd.live.agent.core.parser.TypeReference; -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.stream.Collectors; +import java.io.ByteArrayOutputStream; +import java.io.Reader; +import java.io.Writer; +import java.lang.reflect.Type; @Extension(value = ObjectParser.JSON, provider = "fastjson2") -public class Fastjson2JsonParser extends AbstractFastjson2Parser implements JsonPathParser { +public class Fastjson2JsonParser implements ObjectParser { + + static { + JSONFactory.getDefaultObjectWriterProvider().register(new JoyLiveWriterModule()); + JSONFactory.getDefaultObjectReaderProvider().register(new JoyLiveReaderModule()); + } + + @Override + public T read(Reader reader, Class clazz) { + try { + return com.alibaba.fastjson2.JSON.parseObject(reader, clazz, JSONReader.Feature.FieldBased); + } catch (Exception e) { + throw new ParseException(e.getMessage(), e); + } + } + + @Override + public T read(Reader reader, TypeReference reference) { + try { + return com.alibaba.fastjson2.JSON.parseObject(reader, reference.getType(), JSONReader.Feature.FieldBased); + } catch (Exception e) { + throw new ParseException(e.getMessage(), e); + } + } @Override - public T read(String reader, String path) { - return (T) JSONPath.eval(reader, path); + public T read(Reader reader, Type type) { + try { + return com.alibaba.fastjson2.JSON.parseObject(reader, type, JSONReader.Feature.FieldBased); + } catch (Exception e) { + throw new ParseException(e.getMessage(), e); + } } @Override - public T read(InputStream in, String path) { - return (T) JSONPath.eval( - new BufferedReader(new InputStreamReader(in)) - .lines() - .collect(Collectors.joining(System.lineSeparator())), - path); + public void write(Writer writer, Object obj) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(1000); + try { + com.alibaba.fastjson2.JSON.writeTo(stream, obj, JSONWriter.Feature.FieldBased); + writer.write(stream.toString()); + } catch (Throwable e) { + throw new ParseException(e.getMessage(), e); + } } } diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2JsonPathParser.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2JsonPathParser.java new file mode 100644 index 00000000..fbb90215 --- /dev/null +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2JsonPathParser.java @@ -0,0 +1,50 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.implement.parser.fastjson2; + +import com.alibaba.fastjson2.JSONPath; +import com.jd.live.agent.core.exception.ParseException; +import com.jd.live.agent.core.extension.annotation.Extension; +import com.jd.live.agent.core.parser.JsonPathParser; +import com.jd.live.agent.core.util.IOUtils; + +import java.io.InputStream; + +@Extension(value = "fastjson2", order = 1) +public class Fastjson2JsonPathParser implements JsonPathParser { + + @SuppressWarnings("unchecked") + @Override + public T read(String reader, String path) { + try { + return (T) JSONPath.eval(reader, path); + } catch (Throwable e) { + throw new ParseException(e.getMessage(), e); + } + } + + @SuppressWarnings("unchecked") + @Override + public T read(InputStream in, String path) { + try { + byte[] buffer = IOUtils.read(in); + return (T) JSONPath.eval(new String(buffer), path); + } catch (Throwable e) { + throw new ParseException(e.getMessage(), e); + } + } +} + diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2YamlParser.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2YamlParser.java deleted file mode 100644 index 1b859e56..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2YamlParser.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2; - -import com.jd.live.agent.core.extension.annotation.Extension; -import com.jd.live.agent.core.parser.ObjectParser; - -@Extension(value = {ObjectParser.YAML, ObjectParser.YML}, provider = "fastjson2") -public class Fastjson2YamlParser extends AbstractFastjson2Parser { - @Override - protected String getSupportedType() { - return "YAML"; - } -} \ No newline at end of file diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/JoyLiveReaderModule.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/JoyLiveReaderModule.java index bbae5cb2..6c8339a7 100644 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/JoyLiveReaderModule.java +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/JoyLiveReaderModule.java @@ -1,63 +1,65 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.jd.live.agent.implement.parser.fastjson2; +import com.alibaba.fastjson2.JSONReader; import com.alibaba.fastjson2.codec.FieldInfo; import com.alibaba.fastjson2.modules.ObjectReaderAnnotationProcessor; import com.alibaba.fastjson2.modules.ObjectReaderModule; +import com.alibaba.fastjson2.reader.ObjectReader; import com.alibaba.fastjson2.util.BeanUtils; import com.jd.live.agent.core.parser.json.*; -import com.jd.live.agent.implement.parser.fastjson2.proxy.object.ProxyObjectReader; -import com.jd.live.agent.implement.parser.fastjson2.proxy.time.ProxyTimeReader; import java.lang.annotation.Annotation; -import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.Stack; +import java.lang.reflect.Type; -import static com.alibaba.fastjson2.util.BeanUtils.getAnnotations; +import static com.jd.live.agent.implement.parser.fastjson2.Converters.getConverter; +/** + * A module for the ObjectReader that handles custom annotations for JSON deserialization. + */ public class JoyLiveReaderModule implements ObjectReaderModule { - final JoyLiveReadAnnotationProcessor annotationProcessor; - public JoyLiveReaderModule() { - this.annotationProcessor = new JoyLiveReadAnnotationProcessor(); } public ObjectReaderAnnotationProcessor getAnnotationProcessor() { - return annotationProcessor; + return JoyLiveReadAnnotationProcessor.INSTANCE; } - public class JoyLiveReadAnnotationProcessor implements ObjectReaderAnnotationProcessor { - @Override - public void getFieldInfo(FieldInfo fieldInfo, Class objectClass, Method method) { - String methodName = method.getName(); - String fieldName; - - if (methodName.startsWith("set")) { - fieldName = BeanUtils.setterName(methodName, null); - } else { - fieldName = BeanUtils.getterName(methodName, null); - } + /** + * A private static class that implements the ObjectReaderAnnotationProcessor interface. + */ + private static class JoyLiveReadAnnotationProcessor implements ObjectReaderAnnotationProcessor { - BeanUtils.declaredFields(objectClass, field -> { - if (field.getName().equals(fieldName)) { - int modifiers = field.getModifiers(); - if (!Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)) { - Annotation[] annotations = getAnnotations(field); - processAnnotation(fieldInfo, annotations, field); - } - } - }); - } + private static final JoyLiveReadAnnotationProcessor INSTANCE = new JoyLiveReadAnnotationProcessor(); @Override public void getFieldInfo(FieldInfo fieldInfo, Class objectClass, Field field) { - Annotation[] annotations = getAnnotations(field); - processAnnotation(fieldInfo, annotations, field); + processAnnotation(fieldInfo, BeanUtils.getAnnotations(field), field); } + /** + * Processes the annotations for a given field. + * + * @param fieldInfo The field information. + * @param annotations The annotations on the field. + * @param field The field being processed. + */ private void processAnnotation(FieldInfo fieldInfo, Annotation[] annotations, Field field) { for (Annotation annotation : annotations) { Class annotationType = annotation.annotationType(); @@ -79,64 +81,77 @@ private void processAnnotation(FieldInfo fieldInfo, Annotation[] annotations, Fi } } + /** + * Processes the JsonField annotation on a field. + * + * @param fieldInfo The field information. + * @param jsonField The JsonField annotation. + */ private void processJsonField(FieldInfo fieldInfo, JsonField jsonField) { - String jsonFieldName = jsonField.value(); - if (!jsonFieldName.isEmpty()) { - fieldInfo.fieldName = jsonFieldName; - } + fieldInfo.fieldName = !jsonField.value().isEmpty() ? jsonField.value() : fieldInfo.fieldName; } + /** + * Processes the JsonAlias annotation on a field. + * + * @param fieldInfo The field information. + * @param jsonAlias The JsonAlias annotation. + */ private void processJsonAlias(FieldInfo fieldInfo, JsonAlias jsonAlias) { String[] values = jsonAlias.value(); if (values.length > 0) { - fieldInfo.alternateNames = values; + String[] alternateNames = fieldInfo.alternateNames; + if (alternateNames == null || alternateNames.length == 0) { + fieldInfo.alternateNames = values; + } else { + String[] newValues = new String[alternateNames.length + values.length]; + System.arraycopy(alternateNames, 0, newValues, 0, alternateNames.length); + System.arraycopy(values, 0, newValues, alternateNames.length, values.length); + fieldInfo.alternateNames = newValues; + } } } + /** + * Processes the JsonFormat annotation on a field. + * + * @param fieldInfo The field information. + * @param jsonFormat The JsonFormat annotation. + * @param field The field being processed. + */ private void processJsonFormat(FieldInfo fieldInfo, JsonFormat jsonFormat, Field field) { - String pattern = jsonFormat.pattern(); - String timezone = jsonFormat.timezone(); - String typeName = field.getType().getName(); - String fieldName = field.getName(); - boolean typeMatch = "java.util.Date".equals(typeName) || - "java.util.Calendar".equals(typeName) || - "java.time.ZonedDateTime".equals(typeName) || - "java.time.OffsetDateTime".equals(typeName) || - "java.time.OffsetTime".equals(typeName); - if (typeMatch) { - String cachedKey = field.getDeclaringClass().getName() + "." + field.getName(); - if (!ProxySupport.cachedKey.get().contains(cachedKey)) { - ProxySupport.timeFormatThreadLocal.get().computeIfAbsent(fieldName, key -> new Stack<>()) - .push(new String[]{pattern, timezone}); - ProxySupport.cachedKey.get().add(cachedKey); - } - fieldInfo.readUsing = ProxyTimeReader.class; - } else { - fieldInfo.format = pattern; - } + fieldInfo.format = jsonFormat.pattern(); } + /** + * Processes the DeserializeConverter annotation on a field. + * + * @param fieldInfo The field information. + * @param deserializeConverter The DeserializeConverter annotation. + * @param field The field being processed. + */ private void processDeserializerConverter(FieldInfo fieldInfo, DeserializeConverter deserializeConverter, Field field) { - try { - Class> clazz = deserializeConverter.value(); - String className = clazz.getName(); - - ProxySupport.jsonConverterThreadLocal.get().computeIfAbsent(field.getName(), key -> new Stack<>()) - .push( - ProxySupport.converterMap.computeIfAbsent(className, key -> { - try { - Constructor> constructor = clazz.getConstructor(); - constructor.setAccessible(true); - return constructor.newInstance(); - } catch (Exception e) { - throw new RuntimeException(e.getMessage(), e); - } - }) - ); - fieldInfo.readUsing = ProxyObjectReader.class; - } catch (Exception e) { - throw new RuntimeException(e.getMessage(), e); + Class> clazz = deserializeConverter.value(); + String name = field.getName() + "@" + field.getType().getTypeName(); + Converters.getOrCreateConverter(name, clazz); + fieldInfo.readUsing = ProxyObjectReader.class; + } + } + + /** + * A proxy object reader that uses custom converters for deserialization. + */ + public static class ProxyObjectReader implements ObjectReader { + + @SuppressWarnings("unchecked") + @Override + public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { + String name = fieldName.toString() + "@" + fieldType.getTypeName(); + JsonConverter jsonConverter = (JsonConverter) getConverter(name); + if (jsonConverter != null) { + return jsonConverter.convert(jsonReader.readAny()); } + return null; } } } diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/JoyLiveWriterModule.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/JoyLiveWriterModule.java index cbb40481..5cf65ab4 100644 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/JoyLiveWriterModule.java +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/JoyLiveWriterModule.java @@ -1,64 +1,72 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.jd.live.agent.implement.parser.fastjson2; +import com.alibaba.fastjson2.JSONWriter; import com.alibaba.fastjson2.codec.BeanInfo; import com.alibaba.fastjson2.codec.FieldInfo; import com.alibaba.fastjson2.modules.ObjectWriterAnnotationProcessor; import com.alibaba.fastjson2.modules.ObjectWriterModule; -import com.alibaba.fastjson2.util.BeanUtils; +import com.alibaba.fastjson2.writer.ObjectWriter; import com.jd.live.agent.core.parser.json.JsonConverter; import com.jd.live.agent.core.parser.json.JsonField; import com.jd.live.agent.core.parser.json.JsonFormat; import com.jd.live.agent.core.parser.json.SerializeConverter; -import com.jd.live.agent.implement.parser.fastjson2.proxy.object.ProxyObjectWriter; -import com.jd.live.agent.implement.parser.fastjson2.proxy.time.ProxyTimeWriter; import java.lang.annotation.Annotation; -import java.lang.reflect.Constructor; import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.Stack; +import java.lang.reflect.Type; import static com.alibaba.fastjson2.util.BeanUtils.getAnnotations; +import static com.jd.live.agent.implement.parser.fastjson2.Converters.getConverter; +import static com.jd.live.agent.implement.parser.fastjson2.Converters.getOrCreateConverter; +/** + * A module for the ObjectWriter that handles custom annotations for JSON serialization. + */ public class JoyLiveWriterModule implements ObjectWriterModule { - final JoyLiveWriteAnnotationProcessor annotationProcessor; - - public JoyLiveWriterModule() { - this.annotationProcessor = new JoyLiveWriteAnnotationProcessor(); - } + /** + * Gets the annotation processor for this module. + * + * @return The annotation processor instance. + */ public ObjectWriterAnnotationProcessor getAnnotationProcessor() { - return annotationProcessor; + return JoyLiveWriteAnnotationProcessor.INSTANCE; } - public class JoyLiveWriteAnnotationProcessor implements ObjectWriterAnnotationProcessor { - @Override - public void getFieldInfo(BeanInfo beanInfo, FieldInfo fieldInfo, Class objectClass, Method method) { - String methodName = method.getName(); - String fieldName; - if (methodName.startsWith("set")) { - fieldName = BeanUtils.setterName(methodName, null); - } else { - fieldName = BeanUtils.getterName(methodName, null); - } - BeanUtils.declaredFields(objectClass, field -> { - if (field.getName().equals(fieldName)) { - int modifiers = field.getModifiers(); - if ((!Modifier.isPublic(modifiers)) && !Modifier.isStatic(modifiers)) { - Annotation[] annotations = getAnnotations(field); - processAnnotation(fieldInfo, annotations, field); - } - } - }); - } + /** + * A private static class that implements the ObjectWriterAnnotationProcessor interface. + */ + private static class JoyLiveWriteAnnotationProcessor implements ObjectWriterAnnotationProcessor { + + private static final JoyLiveWriteAnnotationProcessor INSTANCE = new JoyLiveWriteAnnotationProcessor(); @Override public void getFieldInfo(BeanInfo beanInfo, FieldInfo fieldInfo, Class objectClass, Field field) { - Annotation[] annotations = getAnnotations(field); - processAnnotation(fieldInfo, annotations, field); + processAnnotation(fieldInfo, getAnnotations(field), field); } + /** + * Processes the annotations for a given field. + * + * @param fieldInfo The field information. + * @param annotations The annotations on the field. + * @param field The field being processed. + */ private void processAnnotation(FieldInfo fieldInfo, Annotation[] annotations, Field field) { for (Annotation annotation : annotations) { Class annotationType = annotation.annotationType(); @@ -79,55 +87,56 @@ private void processAnnotation(FieldInfo fieldInfo, Annotation[] annotations, Fi } } + /** + * Processes the JsonField annotation on a field. + * + * @param fieldInfo The field information. + * @param jsonField The JsonField annotation. + */ private void processJsonField(FieldInfo fieldInfo, JsonField jsonField) { - String jsonFieldName = jsonField.value(); - if (!jsonFieldName.isEmpty()) { - fieldInfo.fieldName = jsonFieldName; - } + fieldInfo.fieldName = !jsonField.value().isEmpty() ? jsonField.value() : fieldInfo.fieldName; } + /** + * Processes the JsonFormat annotation on a field. + * + * @param fieldInfo The field information. + * @param jsonFormat The JsonFormat annotation. + * @param field The field being processed. + */ private void processJsonFormat(FieldInfo fieldInfo, JsonFormat jsonFormat, Field field) { - String pattern = jsonFormat.pattern(); - String timezone = jsonFormat.timezone(); - String typeName = field.getType().getName(); - String fieldName = field.getName(); - boolean typeMatch = "java.util.Date".equals(typeName) || - "java.util.Calendar".equals(typeName) || - "java.time.ZonedDateTime".equals(typeName) || - "java.time.OffsetDateTime".equals(typeName) || - "java.time.OffsetTime".equals(typeName); - if (typeMatch) { - String cachedKey = field.getDeclaringClass().getName() + "." + field.getName(); - if (!ProxySupport.cachedKey.get().contains(cachedKey)) { - ProxySupport.timeFormatThreadLocal.get().computeIfAbsent(fieldName, key -> new Stack<>()) - .push(new String[]{pattern, timezone}); - ProxySupport.cachedKey.get().add(cachedKey); - } - fieldInfo.writeUsing = ProxyTimeWriter.class; - } else { - fieldInfo.format = pattern; - } + fieldInfo.format = jsonFormat.pattern(); } + /** + * Processes the SerializeConverter annotation on a field. + * + * @param fieldInfo The field information. + * @param serializeConverter The SerializeConverter annotation. + * @param field The field being processed. + */ private void processSerializerConverter(FieldInfo fieldInfo, SerializeConverter serializeConverter, Field field) { - try { - Class> clazz = serializeConverter.value(); - String className = clazz.getName(); - ProxySupport.jsonConverterThreadLocal.get().computeIfAbsent(field.getName(), key -> new Stack<>()) - .push( - ProxySupport.converterMap.computeIfAbsent(className, key -> { - try { - Constructor> constructor = clazz.getConstructor(); - constructor.setAccessible(true); - return constructor.newInstance(); - } catch (Exception e) { - throw new RuntimeException(e.getMessage(), e); - } - }) - ); - fieldInfo.writeUsing = ProxyObjectWriter.class; - } catch (Exception e) { - throw new RuntimeException(e.getMessage(), e); + Class> clazz = serializeConverter.value(); + String name = field.getName() + "@" + field.getType().getTypeName(); + getOrCreateConverter(name, clazz); + fieldInfo.writeUsing = ProxyObjectWriter.class; + } + } + + /** + * A proxy object writer that uses custom converters for serialization. + */ + public static class ProxyObjectWriter implements ObjectWriter { + + @SuppressWarnings("unchecked") + @Override + public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { + String name = fieldName.toString() + "@" + fieldType.getTypeName(); + JsonConverter converter = (JsonConverter) getConverter(name); + if (converter != null) { + jsonWriter.writeAny(converter.convert(object)); + } else { + jsonWriter.writeNull(); } } } diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/ProxySupport.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/ProxySupport.java deleted file mode 100644 index b0d02224..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/ProxySupport.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2; - -import com.alibaba.fastjson2.reader.ObjectReader; -import com.alibaba.fastjson2.writer.ObjectWriter; -import com.jd.live.agent.core.parser.json.JsonConverter; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Stack; -import java.util.concurrent.ConcurrentHashMap; - -public class ProxySupport { - public static ThreadLocal>> jsonConverterThreadLocal = - ThreadLocal.withInitial(HashMap::new); - - public static ThreadLocal>> timeFormatThreadLocal = - ThreadLocal.withInitial(HashMap::new); - - public static ThreadLocal> cachedKey = ThreadLocal.withInitial(HashSet::new); - - public static Map converterMap = new ConcurrentHashMap<>(); - - public static Map> jsonConverterReadCachedMap = new ConcurrentHashMap<>(); - - public static Map> jsonConverterWriteCachedMap = new ConcurrentHashMap<>(); - - public static Map> timeFormatReadCachedMap = new ConcurrentHashMap<>(); - - public static Map> timeFormatWriteCachedMap = new ConcurrentHashMap<>(); -} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/object/ProxyObjectReader.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/object/ProxyObjectReader.java deleted file mode 100644 index da92c24a..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/object/ProxyObjectReader.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2.proxy.object; - -import com.alibaba.fastjson2.JSONReader; -import com.alibaba.fastjson2.reader.ObjectReader; -import com.jd.live.agent.core.parser.json.JsonConverter; -import com.jd.live.agent.implement.parser.fastjson2.ProxySupport; - -import java.lang.reflect.Type; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class ProxyObjectReader implements ObjectReader { - - @Override - public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - JsonConverter jsonConverter; - Map cachedMap = ProxySupport.jsonConverterReadCachedMap.computeIfAbsent(this, - key -> new ConcurrentHashMap<>()); - - if (cachedMap.get(fieldName.toString()) == null) { - jsonConverter = ProxySupport.jsonConverterThreadLocal.get().get(fieldName.toString()).pop(); - cachedMap.put(fieldName.toString(), jsonConverter); - } else { - jsonConverter = cachedMap.get(fieldName.toString()); - } - - if (jsonConverter != null) { - return jsonConverter.convert(jsonReader.readAny()); - } - - return null; - } -} - diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/object/ProxyObjectWriter.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/object/ProxyObjectWriter.java deleted file mode 100644 index a6ff6b43..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/object/ProxyObjectWriter.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2.proxy.object; - -import com.alibaba.fastjson2.JSONWriter; -import com.alibaba.fastjson2.writer.ObjectWriter; -import com.jd.live.agent.core.parser.json.JsonConverter; -import com.jd.live.agent.implement.parser.fastjson2.ProxySupport; - -import java.lang.reflect.Type; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class ProxyObjectWriter implements ObjectWriter { - - @Override - public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { - JsonConverter jsonConverter; - Map cachedMap = ProxySupport.jsonConverterWriteCachedMap.computeIfAbsent(this, - key -> new ConcurrentHashMap<>()); - - if (cachedMap.get(fieldName.toString()) == null) { - jsonConverter = ProxySupport.jsonConverterThreadLocal.get().get(fieldName.toString()).pop(); - cachedMap.put(fieldName.toString(), jsonConverter); - } else { - jsonConverter = cachedMap.get(fieldName.toString()); - } - - if (jsonConverter != null) { - jsonWriter.writeAny(jsonConverter.convert(object)); - } - } -} - diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/ProxyTimeReader.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/ProxyTimeReader.java deleted file mode 100644 index d07fd6ab..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/ProxyTimeReader.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2.proxy.time; - -import com.alibaba.fastjson2.JSONReader; -import com.alibaba.fastjson2.reader.ObjectReader; -import com.jd.live.agent.implement.parser.fastjson2.ProxySupport; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.lang.reflect.Type; -import java.time.ZoneId; -import java.util.Locale; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class ProxyTimeReader implements ObjectReader { - - @Override - public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - ZoneId zoneId = jsonReader.getContext().getZoneId(); - String[] timeInfo; - - try { - Map cachedMap = ProxySupport.timeFormatReadCachedMap.computeIfAbsent(this, - key -> new ConcurrentHashMap<>()); - - if (cachedMap.get(fieldName.toString()) == null) { - timeInfo = ProxySupport.timeFormatThreadLocal.get().get(fieldName.toString()).pop(); - cachedMap.put(fieldName.toString(), timeInfo); - } else { - timeInfo = cachedMap.get(fieldName.toString()); - } - - String className = null; - switch (fieldType.getTypeName()) { - case "java.util.Date": - className = "com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader.ObjectReaderImplDateSZ"; - break; - case "java.util.Calendar": - className = "com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader.ObjectReaderImplCalendarSZ"; - break; - case "java.time.ZonedDateTime": - className = "com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader.ObjectReaderImplZonedDateTimeSZ"; - break; - case "java.time.OffsetDateTime": - className = "com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader.ObjectReaderImplOffsetDateTimeSZ"; - break; - case "java.time.OffsetTime": - className = "com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader.ObjectReaderImplOffsetTimeSZ"; - break; - default: - throw new RuntimeException("unsupported type: " + fieldType.getTypeName()); - } - - String zoneIdString = timeInfo[1]; - if (zoneIdString != null && !zoneIdString.isEmpty()) { - jsonReader.getContext().setZoneId(ZoneId.of(zoneIdString)); - } - - return readObject(timeInfo[0], className, jsonReader, fieldType, fieldName, features); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - jsonReader.getContext().setZoneId(zoneId); - } - } - - private Object readObject(String format, String className, JSONReader jsonReader, Type fieldType, Object fieldName, long features) throws Exception { - Class clazz = Class.forName(className); - Constructor constructor = clazz.getDeclaredConstructor(String.class, Locale.class); - constructor.setAccessible(true); - Object instance = constructor.newInstance(format, null); - Method method = clazz.getDeclaredMethod("readObject", JSONReader.class, Type.class, Object.class, long.class); - method.setAccessible(true); - return method.invoke(instance, jsonReader, fieldType, fieldName, features); - } -} - diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/ProxyTimeWriter.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/ProxyTimeWriter.java deleted file mode 100644 index b646ebd5..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/ProxyTimeWriter.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2.proxy.time; - -import com.alibaba.fastjson2.JSONWriter; -import com.alibaba.fastjson2.writer.ObjectWriter; -import com.jd.live.agent.implement.parser.fastjson2.ProxySupport; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.lang.reflect.Type; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.util.Locale; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -public class ProxyTimeWriter implements ObjectWriter { - - @Override - public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { - ZoneId zoneId = jsonWriter.getContext().getZoneId(); - try { - String[] timeInfo; - Map cachedMap = ProxySupport.timeFormatWriteCachedMap.computeIfAbsent(this, - key -> new ConcurrentHashMap<>()); - - timeInfo = cachedMap.get(fieldName.toString()); - if (timeInfo == null) { - timeInfo = ProxySupport.timeFormatThreadLocal.get().get(fieldName.toString()).pop(); - cachedMap.put(fieldName.toString(), timeInfo); - } - - String zoneIdString = timeInfo[1]; - if (zoneIdString != null && !zoneIdString.isEmpty()) { - jsonWriter.getContext().setZoneId(ZoneId.of(zoneIdString)); - } - - String className = null; - switch (fieldType.getTypeName()) { - case "java.util.Date": - className = "com.jd.live.agent.shaded.com.alibaba.fastjson2.writer.ObjectWriterImplDate"; - break; - case "java.util.Calendar": - className = "com.jd.live.agent.shaded.com.alibaba.fastjson2.writer.ObjectWriterImplCalendar"; - break; - case "java.time.ZonedDateTime": - className = "com.jd.live.agent.shaded.com.alibaba.fastjson2.writer.ObjectWriterImplZonedDateTime"; - if (zoneIdString != null && !zoneIdString.isEmpty()) { - object = ((ZonedDateTime) object).withZoneSameInstant(ZoneId.of(zoneIdString)); - } - break; - case "java.time.OffsetDateTime": - className = "com.jd.live.agent.shaded.com.alibaba.fastjson2.writer.ObjectWriterImplOffsetDateTime"; - if (zoneIdString != null && !zoneIdString.isEmpty()) { - object = ((OffsetDateTime) object).atZoneSameInstant(ZoneId.of(zoneIdString)).toOffsetDateTime(); - } - break; - case "java.time.OffsetTime": - className = "com.jd.live.agent.shaded.com.alibaba.fastjson2.writer.ObjectWriterImplOffsetTime"; - break; - default: - throw new RuntimeException("unsupported type: " + fieldType.getTypeName()); - } - - write(timeInfo[0], className, jsonWriter, object, fieldName, fieldType, features); - } catch (Exception e) { - throw new RuntimeException(e); - } finally { - jsonWriter.getContext().setZoneId(zoneId); - } - } - - private void write(String format, String className, JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) throws Exception { - Class clazz = Class.forName(className); - Constructor constructor = clazz.getDeclaredConstructor(String.class, Locale.class); - constructor.setAccessible(true); - Object instance = constructor.newInstance(format, null); - Method method = clazz.getDeclaredMethod("write", JSONWriter.class, Object.class, Object.class, Type.class, long.class); - method.setAccessible(true); - method.invoke(instance, jsonWriter, object, fieldName, fieldType, features); - } -} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplCalendarSZ.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplCalendarSZ.java deleted file mode 100644 index 936db38b..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplCalendarSZ.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader; - -import com.alibaba.fastjson2.JSONException; -import com.alibaba.fastjson2.JSONReader; -import com.alibaba.fastjson2.codec.DateTimeCodec; -import com.alibaba.fastjson2.reader.ObjectReader; - -import java.lang.reflect.Type; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.ZoneOffset; -import java.util.Calendar; -import java.util.Locale; -import java.util.TimeZone; - -public class ObjectReaderImplCalendarSZ extends DateTimeCodec implements ObjectReader { - - public ObjectReaderImplCalendarSZ(String format, Locale locale) { - super(format, locale); - } - - @Override - public Class getObjectClass() { - return Calendar.class; - } - - @Override - public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - if (jsonReader.isInt()) { - long millis = jsonReader.readInt64Value(); - if (formatUnixTime) { - millis *= 1000; - } - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(millis); - return calendar; - } - - if (jsonReader.readIfNull()) { - return null; - } - - long millis = jsonReader.readMillisFromString(); - if (formatUnixTime) { - millis *= 1000; - } - - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(millis); - return calendar; - } - - @Override - public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - if (jsonReader.isString()) { - if (format != null) { - SimpleDateFormat dateFormat; - if (locale != null) { - dateFormat = new SimpleDateFormat(format, locale); - } else { - dateFormat = new SimpleDateFormat(format); - } - dateFormat.setTimeZone(TimeZone.getTimeZone(jsonReader.getContext().getZoneId())); - try { - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(ZoneOffset.UTC)); - calendar.setTime(dateFormat.parse(jsonReader.readString())); - return calendar; - } catch (ParseException e) { - throw new JSONException(e.getMessage(), e); - } - } - - long millis = jsonReader.readMillisFromString(); - if (millis == 0 && jsonReader.wasNull()) { - return null; - } - - if (formatUnixTime) { - millis *= 1000; - } - - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(millis); - return calendar; - } - - if (jsonReader.readIfNull()) { - return null; - } - - long millis = jsonReader.readMillisFromString(); - if (formatUnixTime || jsonReader.getContext().isFormatUnixTime()) { - millis *= 1000; - } - - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(millis); - return calendar; - } -} - diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplDateSZ.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplDateSZ.java deleted file mode 100644 index 3c571920..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplDateSZ.java +++ /dev/null @@ -1,115 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader; - -import com.alibaba.fastjson2.JSONException; -import com.alibaba.fastjson2.JSONReader; -import com.alibaba.fastjson2.codec.DateTimeCodec; -import com.alibaba.fastjson2.reader.ObjectReader; -import com.alibaba.fastjson2.util.TypeUtils; - -import java.lang.reflect.Type; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; -import java.util.Map; -import java.util.TimeZone; - -public class ObjectReaderImplDateSZ extends DateTimeCodec implements ObjectReader { - - public ObjectReaderImplDateSZ(String format, Locale locale) { - super(format, locale); - } - - @Override - public Class getObjectClass() { - return Date.class; - } - - @Override - public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - return readDate(jsonReader); - } - - @Override - public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - return readDate(jsonReader); - } - - private Object readDate(JSONReader jsonReader) { - if (jsonReader.isInt()) { - long millis = jsonReader.readInt64Value(); - if (formatUnixTime) { - millis *= 1000; - } - return new Date(millis); - } - - if (jsonReader.readIfNull()) { - return null; - } - - if (jsonReader.nextIfNullOrEmptyString()) { - return null; - } - - if (jsonReader.current() == 'n') { - return jsonReader.readNullOrNewDate(); - } - - long millis; - if ((formatUnixTime || formatMillis) && jsonReader.isString()) { - millis = jsonReader.readInt64Value(); - if (formatUnixTime) { - millis *= 1000L; - } - } else if (format != null || useSimpleFormatter) { - if (yyyyMMddhhmmss19) { - if (jsonReader.isSupportSmartMatch()) { - millis = jsonReader.readMillisFromString(); - } else { - millis = jsonReader.readMillis19(); - } - return new Date(millis); - } else { - SimpleDateFormat dateFormat; - if (locale != null) { - dateFormat = new SimpleDateFormat(format, locale); - } else { - dateFormat = new SimpleDateFormat(format); - } - dateFormat.setTimeZone(TimeZone.getTimeZone(jsonReader.getContext().getZoneId())); - try { - return dateFormat.parse(jsonReader.readString()); - } catch (ParseException e) { - throw new JSONException(e.getMessage(), e); - } - } - } else { - if (jsonReader.isDate()) { - return jsonReader.readDate(); - } - - if (jsonReader.isTypeRedirect() && jsonReader.nextIfMatchIdent('"', 'v', 'a', 'l', '"')) { - jsonReader.nextIfMatch(':'); - millis = jsonReader.readInt64Value(); - jsonReader.nextIfObjectEnd(); - jsonReader.setTypeRedirect(false); - } else { - millis = jsonReader.readMillisFromString(); - } - - if (millis == 0 && jsonReader.wasNull()) { - return null; - } - - if (formatUnixTime) { - millis *= 1000; - } - } - return new Date(millis); - } - - public Date createInstance(Map map, long features) { - return TypeUtils.toDate(map); - } -} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplOffsetDateTimeSZ.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplOffsetDateTimeSZ.java deleted file mode 100644 index 79735302..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplOffsetDateTimeSZ.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader; - -import com.alibaba.fastjson2.JSONReader; -import com.alibaba.fastjson2.codec.DateTimeCodec; -import com.alibaba.fastjson2.reader.ObjectReader; - -import java.lang.reflect.Type; -import java.time.*; -import java.util.Locale; - -public class ObjectReaderImplOffsetDateTimeSZ extends DateTimeCodec implements ObjectReader { - - public ObjectReaderImplOffsetDateTimeSZ(String format, Locale locale) { - super(format, locale); - } - - @Override - public Class getObjectClass() { - return OffsetDateTime.class; - } - - @Override - public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - return readObject(jsonReader, fieldType, fieldName, features); - } - - @Override - public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - JSONReader.Context context = jsonReader.getContext(); - - if (jsonReader.isInt()) { - long millis = jsonReader.readInt64Value(); - if (formatUnixTime || context.isFormatUnixTime()) { - millis *= 1000; - } - - Instant instant = Instant.ofEpochMilli(millis); - return instant.atOffset(ZoneOffset.UTC); - } - - if (jsonReader.readIfNull()) { - return null; - } - - if (format == null || yyyyMMddhhmmss19 || formatISO8601) { - return jsonReader.readOffsetDateTime().atZoneSameInstant(ZoneOffset.UTC); - } - - String str = jsonReader.readString(); - if (formatMillis || formatUnixTime) { - long millis = Long.parseLong(str); - if (formatUnixTime) { - millis *= 1000L; - } - - Instant instant = Instant.ofEpochMilli(millis); - return instant.atOffset(ZoneOffset.UTC); - } - - return OffsetDateTime.parse(str, getDateFormatter()); - } -} - diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplOffsetTimeSZ.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplOffsetTimeSZ.java deleted file mode 100644 index 3dd202eb..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplOffsetTimeSZ.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader; - -import com.alibaba.fastjson2.JSONReader; -import com.alibaba.fastjson2.codec.DateTimeCodec; -import com.alibaba.fastjson2.reader.ObjectReader; - -import java.lang.reflect.Type; -import java.time.*; -import java.time.format.DateTimeFormatter; -import java.util.Locale; - -public class ObjectReaderImplOffsetTimeSZ extends DateTimeCodec implements ObjectReader { - - public ObjectReaderImplOffsetTimeSZ(String format, Locale locale) { - super(format, locale); - } - - @Override - public Class getObjectClass() { - return OffsetTime.class; - } - - @Override - public Object readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - return readObject(jsonReader, fieldType, fieldName, features); - } - - @Override - public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - JSONReader.Context context = jsonReader.getContext(); - if (jsonReader.isInt()) { - long millis = jsonReader.readInt64Value(); - if (formatUnixTime || context.isFormatUnixTime()) { - millis *= 1000; - } - Instant instant = Instant.ofEpochMilli(millis); - return instant.atOffset(ZoneOffset.UTC).toOffsetTime(); - } - - if (jsonReader.readIfNull()) { - return null; - } - - if (format == null) { - return jsonReader.readOffsetDateTime(); - } - - String str = jsonReader.readString(); - ZoneId zoneId = context.getZoneId(); - if (formatMillis || formatUnixTime) { - long millis = Long.parseLong(str); - if (formatUnixTime) { - millis *= 1000L; - } - Instant instant = Instant.ofEpochMilli(millis); - return instant.atOffset(ZoneOffset.UTC); - } - DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format).withZone(zoneId); - return OffsetTime.parse(str, formatter); - } -} - diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplZonedDateTimeSZ.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplZonedDateTimeSZ.java deleted file mode 100644 index 95632bd0..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/proxy/time/reader/ObjectReaderImplZonedDateTimeSZ.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.jd.live.agent.implement.parser.fastjson2.proxy.time.reader; - -import com.alibaba.fastjson2.JSONReader; -import com.alibaba.fastjson2.codec.DateTimeCodec; -import com.alibaba.fastjson2.reader.ObjectReader; - -import java.lang.reflect.Type; -import java.time.*; -import java.time.format.DateTimeFormatter; -import java.util.Locale; - -public class ObjectReaderImplZonedDateTimeSZ extends DateTimeCodec implements ObjectReader { - - public ObjectReaderImplZonedDateTimeSZ(String format, Locale locale) { - super(format, locale); - } - - @Override - public Class getObjectClass() { - return ZonedDateTime.class; - } - - @Override - public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { - ZonedDateTime zdt; - if (jsonReader.isInt()) { - long millis = jsonReader.readInt64Value(); - if (formatUnixTime) { - millis *= 1000; - } - Instant instant = Instant.ofEpochMilli(millis); - zdt = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC); - } else { - if (jsonReader.readIfNull()) { - zdt = null; - } else if (format == null || yyyyMMddhhmmss19 || formatISO8601) { - zdt = jsonReader.readZonedDateTime().withZoneSameInstant(ZoneOffset.UTC); - } else { - String str = jsonReader.readString(); - if (formatMillis || formatUnixTime) { - long millis = Long.parseLong(str); - if (formatUnixTime) { - millis *= 1000L; - } - Instant instant = Instant.ofEpochMilli(millis); - zdt = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC); - } else { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format).withZone(jsonReader.getContext().getZoneId()); - zdt = ZonedDateTime.from(formatter.parse(str)).withZoneSameInstant(ZoneOffset.UTC); - } - } - } - - return zdt; - } -} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.ConfigParser b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.ConfigParser deleted file mode 100644 index 2f587989..00000000 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.ConfigParser +++ /dev/null @@ -1,2 +0,0 @@ -com.jd.live.agent.implement.parser.fastjson2.Fastjson2JsonParser -com.jd.live.agent.implement.parser.fastjson2.Fastjson2YamlParser \ No newline at end of file diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.JsonPathParser b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.JsonPathParser index ea217959..4eeeaaf8 100644 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.JsonPathParser +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.JsonPathParser @@ -1 +1 @@ -com.jd.live.agent.implement.parser.fastjson2.Fastjson2JsonParser \ No newline at end of file +com.jd.live.agent.implement.parser.fastjson2.Fastjson2JsonPathParser \ No newline at end of file diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.ObjectParser b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.ObjectParser index 2f587989..5ea35fab 100644 --- a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.ObjectParser +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.ObjectParser @@ -1,2 +1 @@ com.jd.live.agent.implement.parser.fastjson2.Fastjson2JsonParser -com.jd.live.agent.implement.parser.fastjson2.Fastjson2YamlParser \ No newline at end of file diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/FastJson2JsonParserTest.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/FastJson2JsonParserTest.java new file mode 100644 index 00000000..094e6e57 --- /dev/null +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/FastJson2JsonParserTest.java @@ -0,0 +1,42 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.implement.parser.fastjson2; + +import com.jd.live.agent.core.util.IOUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; + +public class FastJson2JsonParserTest { + + @Test + public void testJson() throws IOException { + try (InputStream inputStream = this.getClass().getResourceAsStream("/person.json")) { + Assertions.assertNotNull(inputStream); + byte[] bytes = IOUtils.read(inputStream); + String json = new String(bytes); + Fastjson2JsonParser parser = new Fastjson2JsonParser(); + Person person = parser.read(new StringReader(json), Person.class); + Assertions.assertEquals(person.getSex(), Sex.FEMALE); + Fastjson2JsonPathParser jp = new Fastjson2JsonPathParser(); + Assertions.assertEquals(jp.read(json, "$.name"), "person"); + } + + } +} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/Person.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/Person.java new file mode 100644 index 00000000..920ab943 --- /dev/null +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/Person.java @@ -0,0 +1,52 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.implement.parser.fastjson2; + +import com.jd.live.agent.core.parser.json.DeserializeConverter; + +public class Person { + + private String name; + + private int age; + + @DeserializeConverter(SexConverter.class) + private Sex sex; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public Sex getSex() { + return sex; + } + + public void setSex(Sex sex) { + this.sex = sex; + } +} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/Sex.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/Sex.java new file mode 100644 index 00000000..27ad44b6 --- /dev/null +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/Sex.java @@ -0,0 +1,22 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.implement.parser.fastjson2; + +public enum Sex { + + MALE, FEMALE + +} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/SexConverter.java b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/SexConverter.java new file mode 100644 index 00000000..9e496be6 --- /dev/null +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/java/com/jd/live/agent/implement/parser/fastjson2/SexConverter.java @@ -0,0 +1,29 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.implement.parser.fastjson2; + +import com.jd.live.agent.core.parser.json.JsonConverter; + +public class SexConverter implements JsonConverter { + + @Override + public Sex convert(Integer source) { + if (source == null) { + return null; + } + return source == 1 ? Sex.FEMALE : Sex.MALE; + } +} diff --git a/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/resources/person.json b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/resources/person.json new file mode 100644 index 00000000..4da97e91 --- /dev/null +++ b/joylive-implement/joylive-parser/joylive-parser-fastjson2/src/test/resources/person.json @@ -0,0 +1,5 @@ +{ + "name": "person", + "agent": 41, + "sex": 1 +} \ No newline at end of file diff --git a/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonJsonParser.java b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonJsonParser.java index 1e52f858..858cf465 100644 --- a/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonJsonParser.java +++ b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonJsonParser.java @@ -15,40 +15,10 @@ */ package com.jd.live.agent.implement.parser.jackson; -import com.jayway.jsonpath.JsonPath; -import com.jayway.jsonpath.JsonPathException; -import com.jd.live.agent.core.exception.ParseException; import com.jd.live.agent.core.extension.annotation.Extension; -import com.jd.live.agent.core.parser.JsonPathParser; import com.jd.live.agent.core.parser.ObjectParser; -import java.io.IOException; -import java.io.InputStream; +@Extension(value = ObjectParser.JSON, provider = "jackson", order = 1) +public class JacksonJsonParser extends AbstractJacksonParser { -@Extension(value = ObjectParser.JSON, provider = "jackson") -public class JacksonJsonParser extends AbstractJacksonParser implements JsonPathParser { - - @Override - public T read(String reader, String path) { - if (reader == null || path == null) { - return null; - } - try { - return JsonPath.read(reader, path); - } catch (JsonPathException e) { - throw new ParseException("failed to parse " + path, e); - } - } - - @Override - public T read(InputStream in, String path) { - if (in == null || path == null) { - return null; - } - try { - return JsonPath.read(in, path); - } catch (JsonPathException | IOException e) { - throw new ParseException("failed to parse " + path, e); - } - } } diff --git a/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonJsonPathParser.java b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonJsonPathParser.java new file mode 100644 index 00000000..9d419410 --- /dev/null +++ b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonJsonPathParser.java @@ -0,0 +1,53 @@ +/* + * Copyright © ${year} ${owner} (${email}) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.jd.live.agent.implement.parser.jackson; + +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.JsonPathException; +import com.jd.live.agent.core.exception.ParseException; +import com.jd.live.agent.core.extension.annotation.Extension; +import com.jd.live.agent.core.parser.JsonPathParser; + +import java.io.IOException; +import java.io.InputStream; + +@Extension(value = "jackson") +public class JacksonJsonPathParser implements JsonPathParser { + + @Override + public T read(String reader, String path) { + if (reader == null || path == null) { + return null; + } + try { + return JsonPath.read(reader, path); + } catch (JsonPathException e) { + throw new ParseException("failed to parse " + path, e); + } + } + + @Override + public T read(InputStream in, String path) { + if (in == null || path == null) { + return null; + } + try { + return JsonPath.read(in, path); + } catch (JsonPathException | IOException e) { + throw new ParseException("failed to parse " + path, e); + } + } +} diff --git a/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonYamlParser.java b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonYamlParser.java index 4abca5d7..2161c400 100644 --- a/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonYamlParser.java +++ b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JacksonYamlParser.java @@ -20,7 +20,7 @@ import com.jd.live.agent.core.extension.annotation.Extension; import com.jd.live.agent.core.parser.ObjectParser; -@Extension(value = {ObjectParser.YAML, ObjectParser.YML}, provider = "jackson") +@Extension(value = {ObjectParser.YAML, ObjectParser.YML}, provider = "jackson", order = 1) public class JacksonYamlParser extends AbstractJacksonParser { @Override diff --git a/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JsonAnnotationIntrospector.java b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JsonAnnotationIntrospector.java index ab06fa12..d4a85dac 100644 --- a/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JsonAnnotationIntrospector.java +++ b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/java/com/jd/live/agent/implement/parser/jackson/JsonAnnotationIntrospector.java @@ -26,7 +26,10 @@ import com.jd.live.agent.core.parser.json.*; import java.lang.annotation.Annotation; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; @@ -83,11 +86,10 @@ public Value findFormat(Annotated ann) { JsonFormat format = ann.getAnnotation(JsonFormat.class); if (format != null) { String pattern = format.pattern(); - String timezone = format.timezone(); - if (pattern.isEmpty() && timezone.isEmpty()) { + if (pattern.isEmpty()) { return null; } - return Value.forPattern(pattern).withTimeZone(timezone.isEmpty() ? null : TimeZone.getTimeZone(timezone)); + return Value.forPattern(pattern); } return null; } diff --git a/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.JsonPathParser b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.JsonPathParser index e4b762d3..ddee4769 100644 --- a/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.JsonPathParser +++ b/joylive-implement/joylive-parser/joylive-parser-jackson/src/main/resources/META-INF/services/com.jd.live.agent.core.parser.JsonPathParser @@ -1 +1 @@ -com.jd.live.agent.implement.parser.jackson.JacksonJsonParser \ No newline at end of file +com.jd.live.agent.implement.parser.jackson.JacksonJsonPathParser \ No newline at end of file diff --git a/joylive-package/pom.xml b/joylive-package/pom.xml index d328780f..f2494831 100644 --- a/joylive-package/pom.xml +++ b/joylive-package/pom.xml @@ -51,6 +51,10 @@ com.jd.live joylive-parser-jackson + + com.jd.live + joylive-parser-fastjson2 + com.jd.live joylive-parser-properties diff --git a/joylive-package/src/main/assembly/assembly.xml b/joylive-package/src/main/assembly/assembly.xml index 5c90ea18..f4691440 100644 --- a/joylive-package/src/main/assembly/assembly.xml +++ b/joylive-package/src/main/assembly/assembly.xml @@ -60,6 +60,7 @@ com.jd.live:joylive-function-bkdrhash com.jd.live:joylive-logger-slf4j com.jd.live:joylive-parser-jackson + com.jd.live:joylive-parser-fastjson2 com.jd.live:joylive-parser-properties com.jd.live:joylive-service-file com.jd.live:joylive-service-multilive