-
Notifications
You must be signed in to change notification settings - Fork 496
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
Showing
15 changed files
with
316 additions
and
6 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
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
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
103 changes: 103 additions & 0 deletions
103
core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderRootName.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,103 @@ | ||
package com.alibaba.fastjson2.reader; | ||
|
||
import com.alibaba.fastjson2.JSONException; | ||
import com.alibaba.fastjson2.JSONReader; | ||
import com.alibaba.fastjson2.schema.JSONSchema; | ||
import com.alibaba.fastjson2.util.Fnv; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public final class ObjectReaderRootName<T> | ||
extends ObjectReaderAdapter<T> { | ||
protected final String rootName; | ||
protected final long rootNameHashCode; | ||
|
||
public ObjectReaderRootName( | ||
Class objectClass, | ||
String typeKey, | ||
String typeName, | ||
String rootName, | ||
long features, | ||
JSONSchema schema, | ||
Supplier creator, | ||
Function buildFunction, | ||
Class[] seeAlso, | ||
String[] seeAlsoNames, | ||
Class seeAlsoDefault, | ||
FieldReader[] fieldReaders) { | ||
super( | ||
objectClass, | ||
typeKey, | ||
typeName, | ||
features, | ||
schema, | ||
creator, | ||
buildFunction, | ||
seeAlso, | ||
seeAlsoNames, | ||
seeAlsoDefault, | ||
fieldReaders | ||
); | ||
this.rootName = rootName; | ||
this.rootNameHashCode = rootName == null ? 0 : Fnv.hashCode64(rootName); | ||
} | ||
|
||
public T createInstance(Map map, long features) { | ||
Map object = (Map) map.get(rootName); | ||
if (object == null) { | ||
return null; | ||
} | ||
return super.createInstance(object, features); | ||
} | ||
|
||
@Override | ||
public T readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { | ||
if (jsonReader.nextIfNullOrEmptyString()) { | ||
return null; | ||
} | ||
boolean objectStart = jsonReader.nextIfObjectStart(); | ||
if (!objectStart) { | ||
throw new JSONException(jsonReader.info("read rootName error " + typeName)); | ||
} | ||
|
||
T object = null; | ||
for (int i = 0; ; i++) { | ||
if (jsonReader.nextIfObjectEnd()) { | ||
break; | ||
} | ||
if (rootNameHashCode == jsonReader.readFieldNameHashCode()) { | ||
object = super.readJSONBObject(jsonReader, fieldType, fieldName, features); | ||
} else { | ||
jsonReader.skipValue(); | ||
} | ||
} | ||
return object; | ||
} | ||
|
||
@Override | ||
public T readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) { | ||
if (jsonReader.nextIfNullOrEmptyString()) { | ||
return null; | ||
} | ||
boolean objectStart = jsonReader.nextIfObjectStart(); | ||
if (!objectStart) { | ||
throw new JSONException(jsonReader.info("read rootName error " + typeName)); | ||
} | ||
|
||
T object = null; | ||
for (int i = 0; ; i++) { | ||
if (jsonReader.nextIfObjectEnd()) { | ||
break; | ||
} | ||
if (rootNameHashCode == jsonReader.readFieldNameHashCode()) { | ||
object = super.readObject(jsonReader, fieldType, fieldName, features); | ||
} else { | ||
jsonReader.skipValue(); | ||
} | ||
} | ||
return object; | ||
} | ||
} |
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
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
44 changes: 44 additions & 0 deletions
44
core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterRootName.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,44 @@ | ||
package com.alibaba.fastjson2.writer; | ||
|
||
import com.alibaba.fastjson2.JSONObject; | ||
import com.alibaba.fastjson2.JSONWriter; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
|
||
public final class ObjectWriterRootName<T> | ||
extends ObjectWriterAdapter<T> { | ||
final String rootName; | ||
public ObjectWriterRootName( | ||
Class<T> objectClass, | ||
String typeKey, | ||
String typeName, | ||
String rootName, | ||
long features, | ||
List<FieldWriter> fieldWriters | ||
) { | ||
super(objectClass, typeKey, typeName, features, fieldWriters); | ||
this.rootName = rootName; | ||
} | ||
|
||
public void writeJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { | ||
jsonWriter.startObject(); | ||
jsonWriter.writeName(rootName); | ||
super.writeJSONB(jsonWriter, object, fieldName, fieldType, features); | ||
jsonWriter.endObject(); | ||
} | ||
|
||
public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) { | ||
jsonWriter.startObject(); | ||
jsonWriter.writeName(rootName); | ||
jsonWriter.writeColon(); | ||
super.write(jsonWriter, object, fieldName, fieldType, features); | ||
jsonWriter.endObject(); | ||
} | ||
|
||
public JSONObject toJSONObject(T object, long features) { | ||
return JSONObject.of( | ||
rootName, | ||
super.toJSONObject(object, features)); | ||
} | ||
} |
Oops, something went wrong.