forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* tmp - record investigation / debugging * test - empty record read * test - tests showing different object decoder implementation use Co-authored-by: Maxou <[email protected]> Co-authored-by: xinmiao <>
- Loading branch information
1 parent
6927007
commit 077c4d5
Showing
6 changed files
with
366 additions
and
30 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
114 changes: 114 additions & 0 deletions
114
src/main/java/com/jsoniter/ReflectionRecordDecoder.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,114 @@ | ||
package com.jsoniter; | ||
|
||
import com.jsoniter.spi.*; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ReflectionRecordDecoder extends ReflectionObjectDecoder { | ||
|
||
private boolean useOnlyFieldRecord = false; | ||
|
||
public ReflectionRecordDecoder(ClassInfo classInfo) { | ||
|
||
super(classInfo); | ||
|
||
if (desc.clazz.isRecord() && !desc.fields.isEmpty() && tempCount == 0) { | ||
tempCount = tempIdx; | ||
tempCacheKey = "temp@" + desc.clazz.getName(); | ||
ctorArgsCacheKey = "ctor@" + desc.clazz.getName(); | ||
|
||
desc.ctor.parameters.addAll(desc.fields); | ||
useOnlyFieldRecord = true; | ||
} | ||
} | ||
|
||
@Override | ||
public Decoder create() { | ||
|
||
if (useOnlyFieldRecord) | ||
return new OnlyFieldRecord(); | ||
|
||
if (desc.ctor.parameters.isEmpty()) { | ||
if (desc.bindingTypeWrappers.isEmpty()) { | ||
return new OnlyFieldRecord(); | ||
} else { | ||
return new WithWrapper(); | ||
} | ||
} else { | ||
return new WithCtor(); | ||
} | ||
} | ||
|
||
public class OnlyFieldRecord implements Decoder { | ||
|
||
@Override | ||
public Object decode(JsonIterator iter) throws IOException { | ||
|
||
try { | ||
System.out.println("ONLY FIELD RECORD"); | ||
return decode_(iter); | ||
} catch (RuntimeException e) { | ||
throw e; | ||
} catch (Exception e) { | ||
throw new JsonException(e); | ||
} | ||
} | ||
|
||
private Object decode_(JsonIterator iter) throws Exception { | ||
if (iter.readNull()) { | ||
CodegenAccess.resetExistingObject(iter); | ||
return null; | ||
} | ||
if (iter.tempObjects == null) { | ||
iter.tempObjects = new HashMap<String, Object>(); | ||
} | ||
Object[] temp = (Object[]) iter.tempObjects.get(tempCacheKey); | ||
if (temp == null) { | ||
temp = new Object[tempCount]; | ||
iter.tempObjects.put(tempCacheKey, temp); | ||
} | ||
Arrays.fill(temp, NOT_SET); | ||
if (!CodegenAccess.readObjectStart(iter)) { | ||
if (requiredIdx > 0) { | ||
throw new JsonException("missing required properties: " + collectMissingFields(0)); | ||
} | ||
return createNewObject(iter, temp); | ||
} | ||
Map<String, Object> extra = null; | ||
long tracker = 0L; | ||
Slice fieldName = CodegenAccess.readObjectFieldAsSlice(iter); | ||
Binding binding = allBindings.get(fieldName); | ||
if (binding == null) { | ||
extra = onUnknownProperty(iter, fieldName, extra); | ||
} else { | ||
if (binding.asMissingWhenNotPresent) { | ||
tracker |= binding.mask; | ||
} | ||
temp[binding.idx] = decodeBinding(iter, binding); | ||
} | ||
while (CodegenAccess.nextToken(iter) == ',') { | ||
fieldName = CodegenAccess.readObjectFieldAsSlice(iter); | ||
binding = allBindings.get(fieldName); | ||
if (binding == null) { | ||
extra = onUnknownProperty(iter, fieldName, extra); | ||
} else { | ||
if (binding.asMissingWhenNotPresent) { | ||
tracker |= binding.mask; | ||
} | ||
temp[binding.idx] = decodeBinding(iter, binding); | ||
} | ||
} | ||
if (tracker != expectedTracker) { | ||
throw new JsonException("missing required properties: " + collectMissingFields(tracker)); | ||
} | ||
Object obj = createNewObject(iter, temp.clone()); | ||
setExtra(obj, extra); | ||
applyWrappers(temp, obj); | ||
return obj; | ||
} | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.jsoniter; | ||
|
||
public record SimpleRecord(String field1, String field2) { | ||
public SimpleRecord() { | ||
this(null, null); | ||
} | ||
public SimpleRecord(String field1, String field2) { | ||
this.field1 = field1; | ||
this.field2 = field2; | ||
} | ||
} |
Oops, something went wrong.