Skip to content

Commit

Permalink
[json-core] Add SimpleMapper.properties() method, option to use Prope…
Browse files Browse the repository at this point in the history
…rtyNames optimisation
  • Loading branch information
rbygrave committed Dec 16, 2024
1 parent d532632 commit 037f78d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.json.simple;

import io.avaje.json.JsonAdapter;
import io.avaje.json.PropertyNames;
import io.avaje.json.core.CoreTypes;
import io.avaje.json.stream.JsonStream;

Expand All @@ -21,6 +22,11 @@ final class DSimpleMapper implements SimpleMapper {
this.listType = new DTypeMapper<>(adapters.listAdapter(), jsonStream);
}

@Override
public PropertyNames properties(String... names) {
return jsonStream.properties(names);
}

@Override
public <T> Type<T> type(JsonAdapter<T> myAdapter) {
return new DTypeMapper<>(myAdapter, jsonStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
import io.avaje.json.JsonWriter;
import io.avaje.json.PropertyNames;
import io.avaje.json.stream.JsonStream;

import java.io.InputStream;
Expand Down Expand Up @@ -42,6 +43,14 @@ static Builder builder() {
return new DSimpleMapperBuilder();
}

/**
* Return the property names as PropertyNames.
* <p>
* Provides the option of optimising the writing of json for property names
* by having them already escaped and encoded rather than as plain strings.
*/
PropertyNames properties(String... names);

/**
* Return a mapper for Object with more reading/writing options.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.avaje.json.JsonAdapter;
import io.avaje.json.JsonReader;
import io.avaje.json.JsonWriter;
import io.avaje.json.PropertyNames;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -14,7 +15,8 @@ class CustomAdapter2Test {
@Test
void mapUsingCustomAdapter() {

MyAdapterUsingRaw myAdapter = new MyAdapterUsingRaw();
PropertyNames names = simpleMapper.properties("foo", "bar");
MyAdapterUsingRaw myAdapter = new MyAdapterUsingRaw(names);

SimpleMapper.Type<MyOtherType> type = simpleMapper.type(myAdapter);

Expand All @@ -36,12 +38,21 @@ static class MyOtherType {

static class MyAdapterUsingRaw implements JsonAdapter<MyOtherType> {

private final PropertyNames names;

MyAdapterUsingRaw(PropertyNames names) {
this.names = names;
}

@Override
public void toJson(JsonWriter writer, MyOtherType value) {
writer.beginObject();
writer.name("foo");
writer.beginObject(names);
writer.name(0);
// writer.beginObject();
// writer.name("foo");
writer.value(value.foo);
writer.name("bar");
writer.name(1);
//writer.name("bar");
writer.value(value.bar);
writer.endObject();
}
Expand Down

0 comments on commit 037f78d

Please sign in to comment.