Skip to content

Commit

Permalink
Simplify fastjson2 parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Nov 6, 2024
1 parent 0ce3216 commit 420c430
Show file tree
Hide file tree
Showing 35 changed files with 596 additions and 1,019 deletions.
5 changes: 5 additions & 0 deletions joylive-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<artifactId>joylive-parser-jackson</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.jd.live</groupId>
<artifactId>joylive-parser-fastjson2</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.jd.live</groupId>
<artifactId>joylive-parser-properties</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,14 @@

<properties>
<fastjson2.version>2.0.53</fastjson2.version>
<snakeyaml.version>2.2</snakeyaml.version>
</properties>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>

</dependencies>
<build>
<plugins>
Expand All @@ -35,7 +30,6 @@
<artifactSet>
<includes>
<include>com.alibaba.fastjson2:*</include>
<include>org.yaml:*</include>
</includes>
</artifactSet>
<transformers>
Expand All @@ -56,10 +50,6 @@
<pattern>com.alibaba.fastjson2</pattern>
<shadedPattern>com.jd.live.agent.shaded.com.alibaba.fastjson2</shadedPattern>
</relocation>
<relocation>
<pattern>org.yaml</pattern>
<shadedPattern>com.jd.live.agent.shaded.org.yaml</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<String, JsonConverter<?, ?>> FIELD_CONVERTERS = new ConcurrentHashMap<>();

/**
* A map of converter class names to their corresponding JSON converters.
*/
private static final Map<String, JsonConverter<?, ?>> 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<? extends JsonConverter<?, ?>> clazz) {
return clazz == null ? null : CONVERTERS.computeIfAbsent(clazz.getName(), key -> {
try {
Constructor<? extends JsonConverter<?, ?>> 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<? extends JsonConverter<?, ?>> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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> T read(Reader reader, Class<T> 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> T read(Reader reader, TypeReference<T> 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> T read(String reader, String path) {
return (T) JSONPath.eval(reader, path);
public <T> 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> 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);
}
}
}

Loading

0 comments on commit 420c430

Please sign in to comment.