com.dslplatform.json.runtime.AttributeObjectAlwaysEncoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsl-json-java8 Show documentation
Show all versions of dsl-json-java8 Show documentation
DSL Platform compatible Java JSON library (https://dsl-platform.com)
package com.dslplatform.json.runtime;
import com.dslplatform.json.JsonWriter;
import com.dslplatform.json.Nullable;
import java.nio.charset.Charset;
class AttributeObjectAlwaysEncoder implements JsonWriter.WriteObject {
private static final Charset utf8 = Charset.forName("UTF-8");
private final Settings.Function read;
private final byte[] quotedName;
private final JsonWriter.WriteObject encoder;
AttributeObjectAlwaysEncoder(
final Settings.Function read,
final String name,
final JsonWriter.WriteObject encoder) {
if (read == null) throw new IllegalArgumentException("read can't be null");
if (name == null || name.isEmpty()) throw new IllegalArgumentException("name can't be null");
if (encoder == null) throw new IllegalArgumentException("encoder can't be null");
this.read = read;
quotedName = ("\"" + name + "\":").getBytes(utf8);
this.encoder = encoder;
}
@Override
public void write(final JsonWriter writer, @Nullable final T value) {
final R attr = read.apply(value);
writer.writeAscii(quotedName);
encoder.write(writer, attr);
}
}