Skip to content

Commit

Permalink
feat - handle multiple record constructors by using canonical constru…
Browse files Browse the repository at this point in the history
…ctor by default (#12)
  • Loading branch information
mxyns committed Mar 4, 2022
1 parent 077c4d5 commit 149b796
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/main/java/com/jsoniter/spi/ClassDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,13 @@ private static ConstructorDescriptor getCtor(Class clazz) {
return cctor;
}

private static ConstructorDescriptor getRecordCtor(Class clazz) {
private static ConstructorDescriptor getRecordCtor(Class<?> clazz) {
ConstructorDescriptor cctor = new ConstructorDescriptor();
try {
Constructor<?> ctor = clazz.getDeclaredConstructors()[0];
cctor.ctor = ctor;
for (Type parameter : ctor.getParameterTypes()) {
ClassInfo info = new ClassInfo(parameter);
}
Class<?>[] canonicalParameterTypes = Arrays.stream(clazz.getRecordComponents()).map(RecordComponent::getType).toArray(Class<?>[]::new);
System.out.println("Canonical Parameter Types : ");
System.out.println(Arrays.toString(canonicalParameterTypes));
cctor.ctor = clazz.getDeclaredConstructor(canonicalParameterTypes);
} catch (Exception e) {
cctor.ctor = null;
}
Expand Down
5 changes: 1 addition & 4 deletions src/test/java/com/jsoniter/TestRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.jsoniter.annotation.JsonProperty;
import com.jsoniter.any.Any;
import com.jsoniter.spi.ClassInfo;
import com.jsoniter.spi.EmptyExtension;
import com.jsoniter.spi.JsonException;
import com.jsoniter.spi.JsoniterSpi;
import junit.framework.TestCase;

import java.io.IOException;
Expand Down Expand Up @@ -103,7 +101,6 @@ public void test_record_error() throws IOException {
}
}


public void test_record_withOnlyFieldDecoder() throws IOException {

assertEquals(ReflectionRecordDecoder.OnlyFieldRecord.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord1.class)).getClass());
Expand Down Expand Up @@ -200,7 +197,7 @@ public TestRecord6(int valInt) {

assertEquals(ReflectionRecordDecoder.OnlyFieldRecord.class, ReflectionDecoderFactory.create(new ClassInfo(TestRecord6.class)).getClass());

JsonIterator iter = JsonIterator.parse("{ 'valInt' : 1 }".replace('\'', '"'));
JsonIterator iter = JsonIterator.parse("{ 'val' : 1 }".replace('\'', '"'));
TestRecord6 record = iter.read(TestRecord6.class);

assertNotNull(record);
Expand Down

0 comments on commit 149b796

Please sign in to comment.