Skip to content

Commit

Permalink
Revert #148: does not work (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 20, 2024
1 parent 0ae2527 commit 87efca7
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public class BeanConstructors

protected Constructor<?> _noArgsCtor;

// @since 2.18
protected Constructor<?> _recordCtor;

protected Constructor<?> _intCtor;
protected Constructor<?> _longCtor;
protected Constructor<?> _stringCtor;
Expand All @@ -30,12 +27,6 @@ public BeanConstructors addNoArgsConstructor(Constructor<?> ctor) {
return this;
}

// @since 2.18
public BeanConstructors addRecordConstructor(Constructor<?> ctor) {
_recordCtor = ctor;
return this;
}

public BeanConstructors addIntConstructor(Constructor<?> ctor) {
_intCtor = ctor;
return this;
Expand All @@ -55,9 +46,6 @@ public void forceAccess() {
if (_noArgsCtor != null) {
_noArgsCtor.setAccessible(true);
}
if (_recordCtor != null) {
_recordCtor.setAccessible(true);
}
if (_intCtor != null) {
_intCtor.setAccessible(true);
}
Expand All @@ -76,14 +64,6 @@ protected Object create() throws Exception {
return _noArgsCtor.newInstance((Object[]) null);
}

// @since 2.18
protected Object createRecord(Object[] components) throws Exception {
if (_recordCtor == null) {
throw new IllegalStateException("Class "+_valueType.getName()+" does not have record constructor to use");
}
return _recordCtor.newInstance(components);
}

protected Object create(String str) throws Exception {
if (_stringCtor == null) {
throw new IllegalStateException("Class "+_valueType.getName()+" does not have single-String constructor to use");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ private POJODefinition _introspectDefinition(Class<?> beanType,
} else if (argType == Long.class || argType == Long.TYPE) {
constructors.addLongConstructor(ctor);
}
} else if (RecordsHelpers.isRecordConstructor(beanType, ctor, propsByName)) {
constructors.addRecordConstructor(ctor);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public class BeanReader
*/
protected final BeanConstructors _constructors;

protected final boolean _isRecordType;

/**
* Constructors used for deserialization use case
*
Expand All @@ -58,7 +56,6 @@ public BeanReader(Class<?> type, Map<String, BeanPropertyReader> props,
aliasMapping = Collections.emptyMap();
}
_aliasMapping = aliasMapping;
_isRecordType = RecordsHelpers.isRecordType(type);
}

@Deprecated // since 2.17
Expand Down Expand Up @@ -158,22 +155,6 @@ public Object read(JSONReader r, JsonParser p) throws IOException
return _constructors.create(p.getLongValue());
case START_OBJECT:
{
// [jackson-jr#148] Record deser support (2.18)
if (_isRecordType) {
final List<Object> values = new ArrayList<>();

String propName;
for (; (propName = p.nextFieldName()) != null;) {
BeanPropertyReader prop = findProperty(propName);
if (prop == null) {
handleUnknown(r, p, propName);
continue;
}
values.add(prop.getReader().readNext(r, p));
}
return _constructors.createRecord(values.toArray());
}
// If not Record, need to use default (no-args) Constructors
Object bean = _constructors.create();
String propName;
final Object[] valueBuf = r._setterBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,25 +473,16 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
setter = null;
}
}
if (RecordsHelpers.isRecordType(raw)) {
try {
field = raw.getDeclaredField(rawProp.name);
} catch (NoSuchFieldException e) {
throw new IllegalStateException("Cannot access field '" + rawProp.name
+ "' of record class `" + raw.getName() + "`", e);
// if no setter, field would do as well
if (setter == null) {
if (field == null) {
continue;
}
} else {
// if no setter, field would do as well
if (setter == null) {
if (field == null) {
continue;
}
// fields should always be public, but let's just double-check
if (forceAccess) {
field.setAccessible(true);
} else if (!Modifier.isPublic(field.getModifiers())) {
continue;
}
// fields should always be public, but let's just double-check
if (forceAccess) {
field.setAccessible(true);
} else if (!Modifier.isPublic(field.getModifiers())) {
continue;
}
}

Expand Down
20 changes: 3 additions & 17 deletions jr-record-test/src/test-jdk17/java/jr/Java17RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,9 @@ public class Java17RecordTest extends TestCase
public record Cow(String message, Map<String, String> object) {
}

// [jackson-jr#94]
// [jackson-jr#94]: Record serialization
public void testJava14RecordSerialization() throws Exception {
JSON json = JSON.std;
var expectedDoc = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";
Cow input = new Cow("MOO", Map.of("Foo", "Bar"));

assertEquals(expectedDoc, json.asString(input));
}

// [jackson-jr#148]
public void testJava14RecordDeserialization() throws Exception {
JSON json = JSON.std;
String inputDoc = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";

Cow expected = new Cow("MOO", Map.of("Foo", "Bar"));

Cow actual = json.beanFrom(Cow.class, inputDoc);
assertEquals(expected, actual);
assertEquals("{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}",
JSON.std.asString(new Cow("MOO", Map.of("Foo", "Bar"))));
}
}
5 changes: 0 additions & 5 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,3 @@ Julian Honnen (@jhonnen)
* Contributed fix for #90: `USE_BIG_DECIMAL_FOR_FLOATS` feature not working
when using `JSON.treeFrom()`
(2.17.1)

Tomasz Gawęda (@TomaszGaweda)

* Contributed #148: Add support for Java Record deserialization
(2.18.0)
3 changes: 1 addition & 2 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ Modules:

2.18.0 (not yet released)

#148: Add support for Java Record deserialization
(contributed by Tomasz G)
-

2.17.1 (04-May-2024)

Expand Down

0 comments on commit 87efca7

Please sign in to comment.