-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ce3216
commit 420c430
Showing
35 changed files
with
596 additions
and
1,019 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 0 additions & 124 deletions
124
...2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/AbstractFastjson2Parser.java
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
...rser-fastjson2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Converters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
77 changes: 62 additions & 15 deletions
77
...json2/src/main/java/com/jd/live/agent/implement/parser/fastjson2/Fastjson2JsonParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.