Skip to content

Commit

Permalink
Merge branch '2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 13, 2024
2 parents 0a517e9 + a91b9ec commit 1a78b2c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
6 changes: 3 additions & 3 deletions jr-objects/src/main/java/tools/jackson/jr/ob/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ public enum Feature
USE_IS_GETTERS(true, true),

/**
* Feature that provides serialization support for Groovy & Java 17 records, by allowing
* Feature that provides serialization support for Groovy and Java 17 records, by allowing
* reading of "non-get-getters" in a class, (like for a field named <code>amount</code>
* the getter would be <code>amount()</code>).
*
* @implNote <p>Feature is disabled by default for backward compatibility.</p>
*<p>
* Feature is disabled by default for backward compatibility.
*
* @since 2.17
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,8 @@ private static String decap(String name) {

/**
* Helper method to detect Groovy's problematic metadata accessor type.
*
* @implNote Groovy MetaClass have cyclic reference, and hence the class containing it should not be serialised without
* either removing that reference, or skipping over such references.
* Groovy MetaClass have cyclic reference, and hence the class containing it should not be
* serialized without either removing that reference, or skipping over such references.
*/
protected static boolean isGroovyMetaClass(Class<?> clazz) {
return "groovy.lang.MetaClass".equals(clazz.getName());
Expand Down
41 changes: 25 additions & 16 deletions jr-test-module/src/test/java/Java17RecordTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.JSON.Feature;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;
import junit.framework.TestCase;

/**
* This test is in test module since the JDK version to be tested is higher than other, and hence supports Records.
*/
public class Java17RecordTest {

@Test
public void testJava14RecordSupport() throws IOException {
JSON jsonParser = JSON.builder().enable(Feature.USE_FIELD_MATCHING_GETTERS).build();
var expectedString = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";
Cow expectedObject = new Cow("MOO", Map.of("Foo", "Bar"));
public class Java17RecordTest extends TestCase
{
record Cow(String message, Map<String, String> object) {
}

var json = jsonParser.asString(expectedObject);
Assert.assertEquals(expectedString, json);
// [jackson-jr#94]
public void testJava14RecordSerialization() throws Exception {
//JSON json = JSON.builder().enable(Feature.USE_FIELD_MATCHING_GETTERS).build();
JSON json = JSON.std();
var expectedDoc = "{\"message\":\"MOO\",\"object\":{\"Foo\":\"Bar\"}}";
Cow input = new Cow("MOO", Map.of("Foo", "Bar"));

Cow object = jsonParser.beanFrom(Cow.class, json);
Assert.assertEquals(expectedObject, object);
assertEquals(expectedDoc, json.asString(input);
}

record Cow(String message, Map<String, String> object) {
// [jackson-jr#148]
public void testJava14RecordDeserialization() throws Exception {
//JSON json = JSON.builder().enable(Feature.USE_FIELD_MATCHING_GETTERS).build();
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);
}
}

0 comments on commit 1a78b2c

Please sign in to comment.