I have the following classes:
LocationCustomDataItem.java
public class LocationCustomDataItem {
private String attributeNumber;
private String attributeLabel;
private String attributeValue;
public LocationCustomDataItem() {
}
public LocationCustomDataItem(final String attributeNumber, final String attributeLabel, final String attributeValue) {
this.attributeNumber = attributeNumber;
this.attributeLabel = attributeLabel;
this.attributeValue = attributeValue;
}
/**
* #return the attributeNumber
*/
public String getAttributeNumber() {
return attributeNumber;
}
/**
* #param attributeNumber the attributeNumber to set
*/
public void setAttributeNumber(String attributeNumber) {
this.attributeNumber = attributeNumber;
}
/**
* #return the attributeLabel
*/
public String getAttributeLabel() {
return attributeLabel;
}
/**
* #param attributeLabel the attributeLabel to set
*/
public void setAttributeLabel(String attributeLabel) {
this.attributeLabel = attributeLabel;
}
/**
* #return the attributeValue
*/
public String getAttributeValue() {
return attributeValue;
}
/**
* #param attributeValue the attributeValue to set
*/
public void setAttributeValue(String attributeValue) {
this.attributeValue = attributeValue;
}
#Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, false);
}
#Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
#Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE, false);
}
LocationCustomDataAttributes.java
public class LocationCustomDataAttributes {
private final List<LocationCustomDataItem> locationCustomDataItems;
public LocationCustomDataAttributes() {
this.locationCustomDataItems = new ArrayList<>();
}
public LocationCustomDataAttributes(final List<LocationCustomDataItem> locationCustomDataItems) {
this.locationCustomDataItems = locationCustomDataItems;
}
/**
* #return unmodifiable locationCustomDataItems list
*/
public List<LocationCustomDataItem> getLocationCustomDataItems() {
return Collections.unmodifiableList(this.locationCustomDataItems);
}
/**
* Adds LocationCustoDataItem to internal collection
*
* #param item LocationCustomDataItem to add to item list
*/
public void addItem(final LocationCustomDataItem item) {
this.locationCustomDataItems.add(item);
}
#Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, false);
}
#Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
#Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE, false);
}
and this test json:
{
"locationCustomDataAttributes" : {
"locationCustomDataItems" : [ {
"attributeLabel" : "testLabel1",
"attributeNumber" : "testNumber1",
"attributeValue" : "testLabel1"
}, {
"attributeLabel" : "testLabel2",
"attributeNumber" : "testNumber2",
"attributeValue" : "testLabel2"
} ]
}
}
I'm trying to use an ObjectMapper to convert the json to the object, but it is throwing an unrecognisedPropertyException around "locationCustomDataAttributes":
MapperTest.java
#Test
public void test() throws JsonParseException, JsonMappingException, IOException {
ObjectMapper om =new ObjectMapper();
LocationCustomDataAttributes actual = om.readValue(json, LocationCustomDataAttributes.class);
LocationCustomDataAttributes expected = new LocationCustomDataAttributes();
expected.addItem(new LocationCustomDataItem("testNumber1", "testLabel1", "testValue1"));
expected.addItem(new LocationCustomDataItem("testNumber2", "testLabel2", "testValue2"));
assertThat(actual).isEqualTo(expected);
}
error message:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "locationCustomDataAttributes" (class com.lmig.ci.fnol.transformer.domain.LocationCustomDataAttributes), not marked as ignorable (one known property: "locationCustomDataItems"])
at [Source: {"locationCustomDataAttributes" : {"locationCustomDataItems" : [ {"attributeLabel" : "testLabel1","attributeNumber" : "testNumber1","attributeValue" : "testLabel1"},{"attributeLabel" : "testLabel2","attributeNumber" : "testNumber2","attributeValue" : "testLabel2"}]}}; line: 1, column: 36] (through reference chain: com.lmig.ci.fnol.transformer.domain.LocationCustomDataAttributes["locationCustomDataAttributes"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:62)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:833)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1096)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1467)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1445)
//etc...`
I don't ave a massive amount of experience working with Jackson directly so I haven't annotated the class itself yet, but from previous efforts(e.g. with Spring controllers) the conversion happens automatically without the need for annotations on the class. In this instance that doesn't look like the case, what am I missing from the class, or does the current class structure not lend itself to automatic conversion by Jackson for some reason?
om.readValue(json, LocationCustomDataAttributes.class)
This line asks to parse a LocationCustomDataAttributes object from the JSON document. But JSON actually contains a wrapper object with a property that can be parsed into a LocationCustomDataAttributes object. Solutions:
Create a reader for a wrapped object. E.g.
ObjectReader reader = om
.readerFor(LocationCustomDataAttributes.class)
.withRootName("locationCustomDataAttributes");
LocationCustomDataAttributes actual = reader.readValue(json);
Define a wrapper class
class AttributesWrapper {
private LocationCustomDataAttributes locationCustomDataAttributes;
// add getters, setters, constructors etc
}
and then parse it:
LocationCustomDataAttributes actual = om
.readValue(json, AttributesWrapper.class)
.getLocationCustomDataAttributes();
Related
I'm trying to convert a HashMap<String, Object> to an Avro record. I get this runtime exception when I do a
DataStream<AvroRecord> dsRpvSchema = filteredVlfRPV.flatMap(new MessageFlattener()) .name("ToAvroSchema").uid("ToAvroSchema").startNewChain();
Not sure what the issue is with Flink converting the valid avro record to Flink Avro.
java.lang.IllegalStateException: Expecting type to be a PojoTypeInfo
at org.apache.flink.formats.avro.typeutils.AvroTypeInfo.generateFieldsFromAvroSchema(AvroTypeInfo.java:71)
at org.apache.flink.formats.avro.typeutils.AvroTypeInfo.<init>(AvroTypeInfo.java:55)
at org.apache.flink.formats.avro.utils.AvroKryoSerializerUtils.createAvroTypeInfo(AvroKryoSerializerUtils.java:81)
at org.apache.flink.api.java.typeutils.TypeExtractor.privateGetForClass(TypeExtractor.java:1653)
at org.apache.flink.api.java.typeutils.TypeExtractor.privateGetForClass(TypeExtractor.java:1559)
at org.apache.flink.api.java.typeutils.TypeExtractor.createTypeInfoWithTypeHierarchy(TypeExtractor.java:866)
at org.apache.flink.api.java.typeutils.TypeExtractor.privateCreateTypeInfo(TypeExtractor.java:747)
at org.apache.flink.api.java.typeutils.TypeExtractor.getUnaryOperatorReturnType(TypeExtractor.java:531)
at org.apache.flink.api.java.typeutils.TypeExtractor.getFlatMapReturnTypes(TypeExtractor.java:168)
at org.apache.flink.streaming.api.datastream.DataStream.flatMap(DataStream.java:637)
I do see two questions on this but no answers provided:
Flink Kafka : Expecting type to be a PojoTypeInfo
Deserialize Avro from kafka as SpecificRecord Failing. Expecting type to be a PojoTypeInfo
UPDATE: 02/01/2022. Below is the AvroRecord class definition.
import org.apache.avro.generic.GenericArray;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.util.Utf8;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
#org.apache.avro.specific.AvroGenerated
public class AvroRecord extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = 9071485731787422200L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"AvroRecord\",\"namespace\":\"com.sample.dataplatform.stream.avro\",\"fields\":[{\"name\":\"_id\",\"type\":\"string\",\"doc\":\"car_id\"}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder<AvroRecord> ENCODER =
new BinaryMessageEncoder<AvroRecord>(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder<AvroRecord> DECODER =
new BinaryMessageDecoder<AvroRecord>(MODEL$, SCHEMA$);
/**
* Return the BinaryMessageEncoder instance used by this class.
* #return the message encoder used by this class
*/
public static BinaryMessageEncoder<AvroRecord> getEncoder() {
return ENCODER;
}
/**
* Return the BinaryMessageDecoder instance used by this class.
* #return the message decoder used by this class
*/
public static BinaryMessageDecoder<AvroRecord> getDecoder() {
return DECODER;
}
/**
* Create a new BinaryMessageDecoder instance for this class that uses the specified {#link SchemaStore}.
* #param resolver a {#link SchemaStore} used to find schemas by fingerprint
* #return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
*/
public static BinaryMessageDecoder<AvroRecord> createDecoder(SchemaStore resolver) {
return new BinaryMessageDecoder<AvroRecord>(MODEL$, SCHEMA$, resolver);
}
/**
* Serializes this AvroRecord to a ByteBuffer.
* #return a buffer holding the serialized data for this instance
* #throws java.io.IOException if this instance could not be serialized
*/
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
return ENCODER.encode(this);
}
/**
* Deserializes a AvroRecord from a ByteBuffer.
* #param b a byte buffer holding serialized data for an instance of this class
* #return a AvroRecord instance decoded from the given buffer
* #throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
*/
public static AvroRecord fromByteBuffer(
java.nio.ByteBuffer b) throws java.io.IOException {
return DECODER.decode(b);
}
/** car_id */
private java.lang.CharSequence _id;
/**
* Default constructor. Note that this does not initialize fields
* to their default values from the schema. If that is desired then
* one should use <code>newBuilder()</code>.
*/
public AvroRecord() {}
/**
* All-args constructor.
* #param _id car_id
*/
public AvroRecord(java.lang.CharSequence _id) {
this._id = _id;
}
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
public java.lang.Object get(int field$) {
switch (field$) {
case 0: return _id;
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
}
}
// Used by DatumReader. Applications should not call.
#SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
switch (field$) {
case 0: _id = (java.lang.CharSequence)value$; break;
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
}
}
/**
* Gets the value of the '_id' field.
* #return car_id
*/
public java.lang.CharSequence getId$1() {
return _id;
}
/**
* Sets the value of the '_id' field.
* car_id
* #param value the value to set.
*/
public void setId$1(java.lang.CharSequence value) {
this._id = value;
}
/**
* Creates a new AvroRecord RecordBuilder.
* #return A new AvroRecord RecordBuilder
*/
public static com.sample.dataplatform.stream.avro.AvroRecord.Builder newBuilder() {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder();
}
/**
* Creates a new AvroRecord RecordBuilder by copying an existing Builder.
* #param other The existing builder to copy.
* #return A new AvroRecord RecordBuilder
*/
public static com.sample.dataplatform.stream.avro.AvroRecord.Builder newBuilder(com.sample.dataplatform.stream.avro.AvroRecord.Builder other) {
if (other == null) {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder();
} else {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder(other);
}
}
/**
* Creates a new AvroRecord RecordBuilder by copying an existing AvroRecord instance.
* #param other The existing instance to copy.
* #return A new AvroRecord RecordBuilder
*/
public static com.sample.dataplatform.stream.avro.AvroRecord.Builder newBuilder(com.sample.dataplatform.stream.avro.AvroRecord other) {
if (other == null) {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder();
} else {
return new com.sample.dataplatform.stream.avro.AvroRecord.Builder(other);
}
}
/**
* RecordBuilder for AvroRecord instances.
*/
#org.apache.avro.specific.AvroGenerated
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<AvroRecord>
implements org.apache.avro.data.RecordBuilder<AvroRecord> {
/** car_id */
private java.lang.CharSequence _id;
/** Creates a new Builder */
private Builder() {
super(SCHEMA$);
}
/**
* Creates a Builder by copying an existing Builder.
* #param other The existing Builder to copy.
*/
private Builder(com.sample.dataplatform.stream.avro.AvroRecord.Builder other) {
super(other);
if (isValidValue(fields()[0], other._id)) {
this._id = data().deepCopy(fields()[0].schema(), other._id);
fieldSetFlags()[0] = other.fieldSetFlags()[0];
}
}
/**
* Creates a Builder by copying an existing AvroRecord instance
* #param other The existing instance to copy.
*/
private Builder(com.sample.dataplatform.stream.avro.AvroRecord other) {
super(SCHEMA$);
if (isValidValue(fields()[0], other._id)) {
this._id = data().deepCopy(fields()[0].schema(), other._id);
fieldSetFlags()[0] = true;
}
}
/**
* Gets the value of the '_id' field.
* car_id
* #return The value.
*/
public java.lang.CharSequence getId$1() {
return _id;
}
/**
* Sets the value of the '_id' field.
* car_id
* #param value The value of '_id'.
* #return This builder.
*/
public com.sample.dataplatform.stream.avro.AvroRecord.Builder setId$1(java.lang.CharSequence value) {
validate(fields()[0], value);
this._id = value;
fieldSetFlags()[0] = true;
return this;
}
/**
* Checks whether the '_id' field has been set.
* car_id
* #return True if the '_id' field has been set, false otherwise.
*/
public boolean hasId$1() {
return fieldSetFlags()[0];
}
/**
* Clears the value of the '_id' field.
* car_id
* #return This builder.
*/
public com.sample.dataplatform.stream.avro.AvroRecord.Builder clearId$1() {
_id = null;
fieldSetFlags()[0] = false;
return this;
}
#Override
#SuppressWarnings("unchecked")
public AvroRecord build() {
try {
AvroRecord record = new AvroRecord();
record._id = fieldSetFlags()[0] ? this._id : (java.lang.CharSequence) defaultValue(fields()[0]);
return record;
} catch (org.apache.avro.AvroMissingFieldException e) {
throw e;
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumWriter<AvroRecord>
WRITER$ = (org.apache.avro.io.DatumWriter<AvroRecord>)MODEL$.createDatumWriter(SCHEMA$);
#Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumReader<AvroRecord>
READER$ = (org.apache.avro.io.DatumReader<AvroRecord>)MODEL$.createDatumReader(SCHEMA$);
#Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
#Override protected boolean hasCustomCoders() { return true; }
#Override public void customEncode(org.apache.avro.io.Encoder out)
throws java.io.IOException
{
out.writeString(this._id);
}
#Override public void customDecode(org.apache.avro.io.ResolvingDecoder in)
throws java.io.IOException
{
org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
if (fieldOrder == null) {
this._id = in.readString(this._id instanceof Utf8 ? (Utf8)this._id : null);
} else {
for (int i = 0; i < 1; i++) {
switch (fieldOrder[i].pos()) {
case 0:
this._id = in.readString(this._id instanceof Utf8 ? (Utf8)this._id : null);
break;
default:
throw new java.io.IOException("Corrupt ResolvingDecoder.");
}
}
}
}
}
The AVRO schema (.avsc) file looks like this:
{
"namespace": "com.sample.dataplatform.stream.avro",
"type": "record",
"name": "AvroRecord",
"fields": [
{
"name": "_id",
"type": "string",
"doc": "car_id"
}
]
}
Any help is appreciated!
Following scenario.
Little SpringBoot application with the following classes:
FooDto:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.LinkedList;
import java.util.List;
import org.immutables.value.Value;
#Value.Immutable
#JsonDeserialize(builder = FooDto.Builder.class)
public interface FooDto{
#JsonProperty("fee")
String getFee();
#JsonProperty("fii")
String getFii();
#JsonProperty("url")
String getUrl();
#Value.Default
#JsonProperty("values")
default String[] getValues(){
return new String[0];
}
}
FooJsonMapper:
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
#Component
/*package*/ class FooJsonMapper {
private final ObjectMapper objectMapper;
FooJsonMapper() {
objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
}
public List<FooDto> toDtos(File jsonFile) throws IOException {
return Arrays.asList(objectMapper.readValue(jsonFile, FooDto[].class));
}
}
and a JSON like this:
[
{
"fee":"SOME_MESSAGE",
"fii":"SOME_TECH_MESSAGE",
"url":"/some",
"values":[
"test",
"some"
]
},
{
"fee":"OTHER_MESSAGE",
"fii":"OTHER_TECH_MESSAGE",
"url":"/other",
"values":[
"other",
"some"
]
},
{
"fee":"4711_MESSAGE",
"fii":"4711_TECH_MESSAGE",
"url":"/4711",
"values":[
]
}
]
Somehow when running the code jackson reports the following:
com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `org.some.package.ImmutableFooDto$Builder`, problem: Cannot build FooDto, some of required attributes are not set [fee, fii, url]
at [Source: (File); line: 13, column: 3] (through reference chain: java.lang.Object[][0])
at com.fasterxml.jackson.databind.exc.ValueInstantiationException.from(ValueInstantiationException.java:47) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1907) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.DeserializationContext.handleInstantiationProblem(DeserializationContext.java:1260) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapInstantiationProblem(BeanDeserializerBase.java:1865) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.finishBuild(BuilderBasedDeserializer.java:202) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.deserialize(BuilderBasedDeserializer.java:217) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:214) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:24) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593) ~[jackson-databind-2.12.3.jar:2.12.3]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3413) ~[jackson-databind-2.12.3.jar:2.12.3]
at org.some.package.FooJsonMapper.toDtos(FooJsonMapper.java:25) ~[classes/:na]
I can confirm that the json is correct as the following line of code creates Objects with the data in them:
return Arrays.asList(objectMapper.readValue(jsonFile, Object[].class));
I also tried using #Value.Style(builder = "new") at the FooDto.class as sugested here: https://immutables.github.io/json.html
Jackson: 2.12.3
org.immutables: 2.8.2
EDIT:
As Requested the ImmutableDTO with Builder:
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Generated;
/**
* Immutable implementation of {#link FooDto}.
* <p>
* Use the builder to create immutable instances:
* {#code ImmutableFooDto.builder()}.
*/
#SuppressWarnings("all")
#Generated({"Immutables.generator", "FooDto"})
public final class ImmutableFooDto implements FooDto {
private final String fee;
private final String fii;
private final String url;
private final String[] values;
private ImmutableFooDto(ImmutableFooDto.Builder builder) {
this.fee = builder.fee;
this.fii = builder.fii;
this.url = builder.url;
if (builder.values != null) {
initShim.values(builder.values);
}
this.values = initShim.getValues();
this.initShim = null;
}
private ImmutableFooDto(
String fee,
String fii,
String url,
String[] values) {
this.fee = fee;
this.fii = fii;
this.url = url;
this.values = values;
this.initShim = null;
}
private static final int STAGE_INITIALIZING = -1;
private static final int STAGE_UNINITIALIZED = 0;
private static final int STAGE_INITIALIZED = 1;
private transient volatile InitShim initShim = new InitShim();
private final class InitShim {
private String[] values;
private int valuesStage;
String[] getValues() {
if (valuesStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (valuesStage == STAGE_UNINITIALIZED) {
valuesStage = STAGE_INITIALIZING;
this.values = getValuesInitialize().clone();
valuesStage = STAGE_INITIALIZED;
}
return this.values;
}
void values(String[] values) {
this.values = values;
valuesStage = STAGE_INITIALIZED;
}
private String formatInitCycleMessage() {
ArrayList<String> attributes = new ArrayList<String>();
if (valuesStage == STAGE_INITIALIZING) attributes.add("values");
return "Cannot build FooDto, attribute initializers form cycle" + attributes;
}
}
private String[] getValuesInitialize() {
return FooDto.super.getValues();
}
/**
* #return The value of the {#code fee} attribute
*/
#JsonProperty("fee")
#Override
public String getFee() {
return fee;
}
/**
* #return The value of the {#code fii} attribute
*/
#JsonProperty("fii")
#Override
public String getFii() {
return fii;
}
/**
* #return The value of the {#code url} attribute
*/
#JsonProperty("url")
#Override
public String getUrl() {
return url;
}
/**
* #return A cloned {#code values} array
*/
#JsonProperty("values")
#Override
public String[] getValues() {
InitShim shim = this.initShim;
return shim != null
? shim.getValues().clone()
: this.values.clone();
}
/**
* Copy the current immutable object by setting a value for the {#link FooDto#getFee() fee} attribute.
* An equals check used to prevent copying of the same value by returning {#code this}.
* #param fee A new value for fee
* #return A modified copy of the {#code this} object
*/
public final ImmutableFooDto withFee(String fee) {
if (this.fee.equals(fee)) return this;
String newValue = Objects.requireNonNull(fee, "fee");
return new ImmutableFooDto(newValue, this.fii, this.url, this.values);
}
/**
* Copy the current immutable object by setting a value for the {#link FooDto#getFii() fii} attribute.
* An equals check used to prevent copying of the same value by returning {#code this}.
* #param fii A new value for fii
* #return A modified copy of the {#code this} object
*/
public final ImmutableFooDto withFii(String fii) {
if (this.fii.equals(fii)) return this;
String newValue = Objects.requireNonNull(fii, "fii");
return new ImmutableFooDto(this.fee, newValue, this.url, this.values);
}
/**
* Copy the current immutable object by setting a value for the {#link FooDto#getUrl() url} attribute.
* An equals check used to prevent copying of the same value by returning {#code this}.
* #param url A new value for url
* #return A modified copy of the {#code this} object
*/
public final ImmutableFooDto withUrl(String url) {
if (this.url.equals(url)) return this;
String newValue = Objects.requireNonNull(url, "url");
return new ImmutableFooDto(this.fee, this.fii, newValue, this.values);
}
/**
* Copy the current immutable object with elements that replace the content of {#link FooDto#getValues() values}.
* The array is cloned before being saved as attribute values.
* #param elements The non-null elements for values
* #return A modified copy of {#code this} object
*/
public final ImmutableFooDto withValues(String... elements) {
String[] newValue = elements.clone();
return new ImmutableFooDto(this.fee, this.fii, this.url, newValue);
}
/**
* This instance is equal to all instances of {#code ImmutableFooDto} that have equal attribute values.
* #return {#code true} if {#code this} is equal to {#code another} instance
*/
#Override
public boolean equals(Object another) {
if (this == another) return true;
return another instanceof ImmutableFooDto
&& equalTo((ImmutableFooDto) another);
}
private boolean equalTo(ImmutableFooDto another) {
return fee.equals(another.fee)
&& fii.equals(another.fii)
&& url.equals(another.url)
&& Arrays.equals(values, another.values);
}
/**
* Computes a hash code from attributes: {#code fee}, {#code fii}, {#code url}, {#code values}, {#code roles}.
* #return hashCode value
*/
#Override
public int hashCode() {
int h = 31;
h = h * 17 + fee.hashCode();
h = h * 17 + fii.hashCode();
h = h * 17 + url.hashCode();
h = h * 17 + Arrays.hashCode(values);
return h;
}
/**
* Prints the immutable value {#code FooDto} with attribute values.
* #return A string representation of the value
*/
#Override
public String toString() {
return "FooDto{"
+ "fee=" + fee
+ ", fii=" + fii
+ ", url=" + url
+ ", values=" + Arrays.toString(values)
+ "}";
}
/**
* Utility type used to correctly read immutable object from JSON representation.
* #deprecated Do not use this type directly, it exists only for the <em>Jackson</em>-binding infrastructure
*/
#Deprecated
#JsonDeserialize
#JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE)
static final class Json implements FooDto {
String fee;
String fii;
String url;
String[] values;
List<String> roles = Collections.emptyList();
boolean rolesIsSet;
#JsonProperty("fee")
public void setFee(String fee) {
this.fee = fee;
}
#JsonProperty("fii")
public void setFii(String fii) {
this.fii = fii;
}
#JsonProperty("url")
public void setUrl(String url) {
this.url = url;
}
#JsonProperty("values")
public void setValues(String[] values) {
this.values = values;
}
#Override
public String getFee() { throw new UnsupportedOperationException(); }
#Override
public String getFii() { throw new UnsupportedOperationException(); }
#Override
public String getUrl() { throw new UnsupportedOperationException(); }
#Override
public String[] getValues() { throw new UnsupportedOperationException(); }
}
/**
* #param json A JSON-bindable data structure
* #return An immutable value type
* #deprecated Do not use this method directly, it exists only for the <em>Jackson</em>-binding infrastructure
*/
#Deprecated
#JsonCreator
static ImmutableFooDto fromJson(Json json) {
ImmutableFooDto.Builder builder = ImmutableFooDto.builder();
if (json.fee != null) {
builder.fee(json.fee);
}
if (json.fii != null) {
builder.fii(json.fii);
}
if (json.url != null) {
builder.url(json.url);
}
if (json.values != null) {
builder.values(json.values);
}
return builder.build();
}
/**
* Creates an immutable copy of a {#link FooDto} value.
* Uses accessors to get values to initialize the new immutable instance.
* If an instance is already immutable, it is returned as is.
* #param instance The instance to copy
* #return A copied immutable FooDto instance
*/
public static ImmutableFooDto copyOf(FooDto instance) {
if (instance instanceof ImmutableFooDto) {
return (ImmutableFooDto) instance;
}
return ImmutableFooDto.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {#link ImmutableFooDto ImmutableFooDto}.
* #return A new ImmutableFooDto builder
*/
public static ImmutableFooDto.Builder builder() {
return new ImmutableFooDto.Builder();
}
/**
* Builds instances of type {#link ImmutableFooDto ImmutableFooDto}.
* Initialize attributes and then invoke the {#link #build()} method to create an
* immutable instance.
* <p><em>{#code Builder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.</em>
*/
public static final class Builder {
private static final long INIT_BIT_TITLE = 0x1L;
private static final long INIT_BIT_TECHNICAL_KEY = 0x2L;
private static final long INIT_BIT_URL = 0x4L;
private long initBits = 0x7L;
private long optBits;
private String fee;
private String fii;
private String url;
private String[] values;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {#code FooDto} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* #param instance The instance from which to copy values
* #return {#code this} builder for use in a chained invocation
*/
public final Builder from(FooDto instance) {
Objects.requireNonNull(instance, "instance");
fee(instance.getFee());
fii(instance.getFii());
url(instance.getUrl());
values(instance.getValues());
return this;
}
/**
* Initializes the value for the {#link FooDto#getFee() fee} attribute.
* #param fee The value for fee
* #return {#code this} builder for use in a chained invocation
*/
public final Builder fee(String fee) {
this.fee = Objects.requireNonNull(fee, "fee");
initBits &= ~INIT_BIT_TITLE;
return this;
}
/**
* Initializes the value for the {#link FooDto#getFii() fii} attribute.
* #param fii The value for fii
* #return {#code this} builder for use in a chained invocation
*/
public final Builder fii(String fii) {
this.fii = Objects.requireNonNull(fii, "fii");
initBits &= ~INIT_BIT_TECHNICAL_KEY;
return this;
}
/**
* Initializes the value for the {#link FooDto#getUrl() url} attribute.
* #param url The value for url
* #return {#code this} builder for use in a chained invocation
*/
public final Builder url(String url) {
this.url = Objects.requireNonNull(url, "url");
initBits &= ~INIT_BIT_URL;
return this;
}
/**
* Initializes the value for the {#link FooDto#getValues() values} attribute.
* <p><em>If not set, this attribute will have a default value as defined by {#link FooDto#getValues() values}.</em>
* #param values The elements for values
* #return {#code this} builder for use in a chained invocation
*/
public final Builder values(String... values) {
this.values = values.clone();
return this;
}
/**
* Builds a new {#link ImmutableFooDto ImmutableFooDto}.
* #return An immutable instance of FooDto
* #throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableFooDto build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new ImmutableFooDto(this);
}
private String formatRequiredAttributesMessage() {
List<String> attributes = new ArrayList<String>();
if ((initBits & INIT_BIT_TITLE) != 0) attributes.add("fee");
if ((initBits & INIT_BIT_TECHNICAL_KEY) != 0) attributes.add("fii");
if ((initBits & INIT_BIT_URL) != 0) attributes.add("url");
return "Cannot build FooDto, some of required attributes are not set " + attributes;
}
}
private static <T> List<T> createSafeList(Iterable<? extends T> iterable, boolean checkNulls, boolean skipNulls) {
ArrayList<T> list;
if (iterable instanceof Collection<?>) {
int size = ((Collection<?>) iterable).size();
if (size == 0) return Collections.emptyList();
list = new ArrayList<T>();
} else {
list = new ArrayList<T>();
}
for (T element : iterable) {
if (skipNulls && element == null) continue;
if (checkNulls) Objects.requireNonNull(element, "element");
list.add(element);
}
return list;
}
private static <T> List<T> createUnmodifiableList(boolean clone, List<T> list) {
switch(list.size()) {
case 0: return Collections.emptyList();
case 1: return Collections.singletonList(list.get(0));
default:
if (clone) {
return Collections.unmodifiableList(new ArrayList<T>(list));
} else {
if (list instanceof ArrayList<?>) {
((ArrayList<?>) list).trimToSize();
}
return Collections.unmodifiableList(list);
}
}
}
}
I think the problem is that jackson can't find a way to construct your class.
Here's what works for me
import org.immutables.value.Value;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
#Value.Immutable
#Value.Style(privateNoargConstructor = true) //<<<< this is what you need I think
#JsonSerialize(as = ImmutableVal.class)
#JsonDeserialize(as = ImmutableVal.class)
interface Val {
int a();
#JsonProperty("b")
String second();
}
and
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ValMain {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(
ImmutableVal.builder()
.a(1)
.second("B")
.build());
System.out.println(json);
//{"a":1,"b":"B"}
Val val = objectMapper.readValue(json, Val.class);
System.out.println(val);
//Val{a=1, second=B}
}
}
Then you can even get rid of the DTOs
I am looking for a way to parse a JSON file for a specific node and get that node's line number in the file. I would like to use the Jayway JSONPath library to support extended JSONPath queries.
For example (from jsonpath.com), here's some JSON:
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}
and here's a jsonPath:
$.phoneNumbers.[?(#.type=='iPhone')]
I would like to have a way to say that this node is on line 11 in the json file. I don't know ahead of time what the json contents might be or the jsonPath. Both are dynamic.
So far, I've tried to parse the json into a tree and traverse it up to the node to get the parser's current location, but the parser must always run to the end of the file before the jsonPath executes. Any other ideas?
I eventually found a solution that involves using Jackson's JsonFactory and JsonParser. It's kludge-y to say the least, but it uses the JsonParser's knowledge of its parser's line number to get the JsonNode's position and works pretty well.
I'll paste the code here, but the code is also available at watchtower github
Calling class:
void findLineNumber() throws Exception{
CustomParserFactory customParserFactory = new CustomParserFactory();
ObjectMapper om = new ObjectMapper(customParserFactory);
factory = new CustomJsonNodeFactory(om.getDeserializationConfig().getNodeFactory(),
customParserFactory);
om.setConfig(om.getDeserializationConfig().with(factory));
config = Configuration.builder()
.mappingProvider(new JacksonMappingProvider(om))
.jsonProvider(new JacksonJsonNodeJsonProvider(om))
.options(Option.ALWAYS_RETURN_LIST)
.build();
File filePath = ...;
JsonPath jsonPath = ...;
DocumentContext parsedDocument = JsonPath.parse(filePath, config);
ArrayNode findings = parsedDocument.read(jsonPath);
for (JsonNode finding : findings) {
JsonLocation location = factory.getLocationForNode(finding);
int lineNum = location.getLineNr();
//Do something with lineNum
}
}
CustomJsonNodeFactory.java
public class CustomJsonNodeFactory extends JsonNodeFactory {
private static final long serialVersionUID = 8807395553661461181L;
private final JsonNodeFactory delegate;
private final CustomParserFactory parserFactory;
/*
* "Why isn't this a map?" you might be wondering. Well, when the nodes are created, they're all
* empty and a node's hashCode is based on its children. So if you use a map and put the node
* in, then the node's hashCode is based on no children, then when you lookup your node, it is
* *with* children, so the hashcodes are different. Instead of all of this, you have to iterate
* through a listing and find their matches once the objects have been populated, which is only
* after the document has been completely parsed
*/
private List<Entry<JsonNode, JsonLocation>> locationMapping;
public CustomJsonNodeFactory(JsonNodeFactory nodeFactory,
CustomParserFactory parserFactory) {
delegate = nodeFactory;
this.parserFactory = parserFactory;
locationMapping = new ArrayList<>();
}
/**
* Given a node, find its location, or null if it wasn't found
*
* #param jsonNode the node to search for
* #return the location of the node or null if not found
*/
public JsonLocation getLocationForNode(JsonNode jsonNode) {
return this.locationMapping.stream().filter(e -> e.getKey().equals(jsonNode))
.map(e -> e.getValue()).findAny().orElse(null);
}
/**
* Simple interceptor to mark the node in the lookup list and return it back
*
* #param <T> the type of the JsonNode
* #param node the node itself
* #return the node itself, having marked its location
*/
private <T extends JsonNode> T markNode(T node) {
JsonLocation loc = parserFactory.getParser().getCurrentLocation();
locationMapping.add(new SimpleEntry<>(node, loc));
return node;
}
#Override
public BooleanNode booleanNode(boolean v) {
return markNode(delegate.booleanNode(v));
}
#Override
public NullNode nullNode() {
return markNode(delegate.nullNode());
}
#Override
public NumericNode numberNode(byte v) {
return markNode(delegate.numberNode(v));
}
#Override
public ValueNode numberNode(Byte value) {
return markNode(delegate.numberNode(value));
}
#Override
public NumericNode numberNode(short v) {
return markNode(delegate.numberNode(v));
}
#Override
public ValueNode numberNode(Short value) {
return markNode(delegate.numberNode(value));
}
#Override
public NumericNode numberNode(int v) {
return markNode(delegate.numberNode(v));
}
#Override
public ValueNode numberNode(Integer value) {
return markNode(delegate.numberNode(value));
}
#Override
public NumericNode numberNode(long v) {
return markNode(delegate.numberNode(v));
}
#Override
public ValueNode numberNode(Long value) {
return markNode(delegate.numberNode(value));
}
#Override
public ValueNode numberNode(BigInteger v) {
return markNode(delegate.numberNode(v));
}
#Override
public NumericNode numberNode(float v) {
return markNode(delegate.numberNode(v));
}
#Override
public ValueNode numberNode(Float value) {
return markNode(delegate.numberNode(value));
}
#Override
public NumericNode numberNode(double v) {
return markNode(delegate.numberNode(v));
}
#Override
public ValueNode numberNode(Double value) {
return markNode(delegate.numberNode(value));
}
#Override
public ValueNode numberNode(BigDecimal v) {
return markNode(delegate.numberNode(v));
}
#Override
public TextNode textNode(String text) {
return markNode(delegate.textNode(text));
}
#Override
public BinaryNode binaryNode(byte[] data) {
return markNode(delegate.binaryNode(data));
}
#Override
public BinaryNode binaryNode(byte[] data, int offset, int length) {
return markNode(delegate.binaryNode(data, offset, length));
}
#Override
public ValueNode pojoNode(Object pojo) {
return markNode(delegate.pojoNode(pojo));
}
#Override
public ValueNode rawValueNode(RawValue value) {
return markNode(delegate.rawValueNode(value));
}
#Override
public ArrayNode arrayNode() {
return markNode(delegate.arrayNode());
}
#Override
public ArrayNode arrayNode(int capacity) {
return markNode(delegate.arrayNode(capacity));
}
#Override
public ObjectNode objectNode() {
return markNode(delegate.objectNode());
}
}
CustomParserFactory.java (Note that this removes thread-safety, which can be kind of a big deal):
public class CustomParserFactory extends JsonFactory {
private static final long serialVersionUID = -7523974986510864179L;
private JsonParser parser;
public JsonParser getParser() {
return this.parser;
}
#Override
public JsonParser createParser(Reader r) throws IOException, JsonParseException {
parser = super.createParser(r);
return parser;
}
#Override
public JsonParser createParser(String content) throws IOException, JsonParseException {
parser = super.createParser(content);
return parser;
}
}
I guess you could use the pretty print version of the json, which should return you the json formatted with the line breaks, and work from there.
I'm having a strange issue with marshalling/unmarshalling an avro generated class. The error I'm getting is throwing a not an enum error - except there aren't any enum's in my class.
The error is specifically this:
com.fasterxml.jackson.databind.JsonMappingException: Not an enum: {"type":"record","name":"TimeUpdateTopic","namespace":"org.company.mmd.time","fields":[{"name":"time","type":"double"}]} (through reference chain: org.company.mmd.time.TimeUpdateTopic["schema"]->org.apache.avro.Schema$RecordSchema["enumDefault"])
Test Case
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.junit.Test
class TimeUpdateTopicTest {
val objectMapper = ObjectMapper().registerModule(JavaTimeModule())
#Test
fun decode() {
val t = TimeUpdateTopic(1.0)
objectMapper.writeValueAsString(t)
}
}
AVDL
#namespace("org.company.mmd.time")
protocol TimeMonitor {
record TimeUpdateTopic {
double time;
}
}
Java class generated by the avro task
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package org.company.mmd.time;
import org.apache.avro.generic.GenericArray;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.util.Utf8;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
#org.apache.avro.specific.AvroGenerated
public class TimeUpdateTopic extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
private static final long serialVersionUID = -4648318619505855037L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"TimeUpdateTopic\",\"namespace\":\"org.company.mmd.time\",\"fields\":[{\"name\":\"time\",\"type\":\"double\"}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
private static SpecificData MODEL$ = new SpecificData();
private static final BinaryMessageEncoder<TimeUpdateTopic> ENCODER =
new BinaryMessageEncoder<TimeUpdateTopic>(MODEL$, SCHEMA$);
private static final BinaryMessageDecoder<TimeUpdateTopic> DECODER =
new BinaryMessageDecoder<TimeUpdateTopic>(MODEL$, SCHEMA$);
/**
* Return the BinaryMessageEncoder instance used by this class.
* #return the message encoder used by this class
*/
public static BinaryMessageEncoder<TimeUpdateTopic> getEncoder() {
return ENCODER;
}
/**
* Return the BinaryMessageDecoder instance used by this class.
* #return the message decoder used by this class
*/
public static BinaryMessageDecoder<TimeUpdateTopic> getDecoder() {
return DECODER;
}
/**
* Create a new BinaryMessageDecoder instance for this class that uses the specified {#link SchemaStore}.
* #param resolver a {#link SchemaStore} used to find schemas by fingerprint
* #return a BinaryMessageDecoder instance for this class backed by the given SchemaStore
*/
public static BinaryMessageDecoder<TimeUpdateTopic> createDecoder(SchemaStore resolver) {
return new BinaryMessageDecoder<TimeUpdateTopic>(MODEL$, SCHEMA$, resolver);
}
/**
* Serializes this TimeUpdateTopic to a ByteBuffer.
* #return a buffer holding the serialized data for this instance
* #throws java.io.IOException if this instance could not be serialized
*/
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
return ENCODER.encode(this);
}
/**
* Deserializes a TimeUpdateTopic from a ByteBuffer.
* #param b a byte buffer holding serialized data for an instance of this class
* #return a TimeUpdateTopic instance decoded from the given buffer
* #throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
*/
public static TimeUpdateTopic fromByteBuffer(
java.nio.ByteBuffer b) throws java.io.IOException {
return DECODER.decode(b);
}
#Deprecated public double time;
/**
* Default constructor. Note that this does not initialize fields
* to their default values from the schema. If that is desired then
* one should use <code>newBuilder()</code>.
*/
public TimeUpdateTopic() {}
/**
* All-args constructor.
* #param time The new value for time
*/
public TimeUpdateTopic(java.lang.Double time) {
this.time = time;
}
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
public java.lang.Object get(int field$) {
switch (field$) {
case 0: return time;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
// Used by DatumReader. Applications should not call.
#SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
switch (field$) {
case 0: time = (java.lang.Double)value$; break;
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
/**
* Gets the value of the 'time' field.
* #return The value of the 'time' field.
*/
public double getTime() {
return time;
}
/**
* Sets the value of the 'time' field.
* #param value the value to set.
*/
public void setTime(double value) {
this.time = value;
}
/**
* Creates a new TimeUpdateTopic RecordBuilder.
* #return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder() {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
}
/**
* Creates a new TimeUpdateTopic RecordBuilder by copying an existing Builder.
* #param other The existing builder to copy.
* #return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder(org.company.mmd.time.TimeUpdateTopic.Builder other) {
if (other == null) {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
} else {
return new org.company.mmd.time.TimeUpdateTopic.Builder(other);
}
}
/**
* Creates a new TimeUpdateTopic RecordBuilder by copying an existing TimeUpdateTopic instance.
* #param other The existing instance to copy.
* #return A new TimeUpdateTopic RecordBuilder
*/
public static org.company.mmd.time.TimeUpdateTopic.Builder newBuilder(org.company.mmd.time.TimeUpdateTopic other) {
if (other == null) {
return new org.company.mmd.time.TimeUpdateTopic.Builder();
} else {
return new org.company.mmd.time.TimeUpdateTopic.Builder(other);
}
}
/**
* RecordBuilder for TimeUpdateTopic instances.
*/
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<TimeUpdateTopic>
implements org.apache.avro.data.RecordBuilder<TimeUpdateTopic> {
private double time;
/** Creates a new Builder */
private Builder() {
super(SCHEMA$);
}
/**
* Creates a Builder by copying an existing Builder.
* #param other The existing Builder to copy.
*/
private Builder(org.company.mmd.time.TimeUpdateTopic.Builder other) {
super(other);
if (isValidValue(fields()[0], other.time)) {
this.time = data().deepCopy(fields()[0].schema(), other.time);
fieldSetFlags()[0] = other.fieldSetFlags()[0];
}
}
/**
* Creates a Builder by copying an existing TimeUpdateTopic instance
* #param other The existing instance to copy.
*/
private Builder(org.company.mmd.time.TimeUpdateTopic other) {
super(SCHEMA$);
if (isValidValue(fields()[0], other.time)) {
this.time = data().deepCopy(fields()[0].schema(), other.time);
fieldSetFlags()[0] = true;
}
}
/**
* Gets the value of the 'time' field.
* #return The value.
*/
public double getTime() {
return time;
}
/**
* Sets the value of the 'time' field.
* #param value The value of 'time'.
* #return This builder.
*/
public org.company.mmd.time.TimeUpdateTopic.Builder setTime(double value) {
validate(fields()[0], value);
this.time = value;
fieldSetFlags()[0] = true;
return this;
}
/**
* Checks whether the 'time' field has been set.
* #return True if the 'time' field has been set, false otherwise.
*/
public boolean hasTime() {
return fieldSetFlags()[0];
}
/**
* Clears the value of the 'time' field.
* #return This builder.
*/
public org.company.mmd.time.TimeUpdateTopic.Builder clearTime() {
fieldSetFlags()[0] = false;
return this;
}
#Override
#SuppressWarnings("unchecked")
public TimeUpdateTopic build() {
try {
TimeUpdateTopic record = new TimeUpdateTopic();
record.time = fieldSetFlags()[0] ? this.time : (java.lang.Double) defaultValue(fields()[0]);
return record;
} catch (org.apache.avro.AvroMissingFieldException e) {
throw e;
} catch (java.lang.Exception e) {
throw new org.apache.avro.AvroRuntimeException(e);
}
}
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumWriter<TimeUpdateTopic>
WRITER$ = (org.apache.avro.io.DatumWriter<TimeUpdateTopic>)MODEL$.createDatumWriter(SCHEMA$);
#Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
#SuppressWarnings("unchecked")
private static final org.apache.avro.io.DatumReader<TimeUpdateTopic>
READER$ = (org.apache.avro.io.DatumReader<TimeUpdateTopic>)MODEL$.createDatumReader(SCHEMA$);
#Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
#Override protected boolean hasCustomCoders() { return true; }
#Override public void customEncode(org.apache.avro.io.Encoder out)
throws java.io.IOException
{
out.writeDouble(this.time);
}
#Override public void customDecode(org.apache.avro.io.ResolvingDecoder in)
throws java.io.IOException
{
org.apache.avro.Schema.Field[] fieldOrder = in.readFieldOrderIfDiff();
if (fieldOrder == null) {
this.time = in.readDouble();
} else {
for (int i = 0; i < 1; i++) {
switch (fieldOrder[i].pos()) {
case 0:
this.time = in.readDouble();
break;
default:
throw new java.io.IOException("Corrupt ResolvingDecoder.");
}
}
}
}
}
Am I doing something stupid and/or wrong here? Or is this an actual bug
Updates
I'm able to get JSON out using this function:
inline fun <reified T: SpecificRecordBase> StringFromAvroGenerated(obj: T) : String {
val schema = obj.schema
val writer = SpecificDatumWriter(T::class.java)
val stream = ByteArrayOutputStream()
var jsonEncoder = EncoderFactory.get().jsonEncoder(schema, stream)
writer.write(obj, jsonEncoder)
jsonEncoder.flush()
return stream.toString("UTF-8")
}
but I was assuming this should be automatic with Jackson
So it appears there are two ways to solve my issues (thanks to JsonMappingException when serializing avro generated object to json)
Write a Jackson MixIn to handle the getSchema call
So the first option required me to create a Mixin such as this:
abstract class AvroMixIn {
#JsonIgnore
abstract fun getSchema(): org.apache.avro.Schema
#JsonIgnore
abstract fun getSpecificData() : org.apache.avro.specific.SpecificData
}
And then when i make an object mapper:
val objectMapper = ObjectMapper()
.registerModule(JavaTimeModule())
.addMixIn(Object::class.java, AvroMixIn::class.java)
I chose Object::class.java instead of the actual class because it should apply to all classes. Probably a better solution is to apply it to a shared base-class all the AvroGenerated stuff has.
Rewrite the Avro Velocity Templates to automatically add this
This is actually the 1st approach I took because it seemed more "seemless".
1) Check out the avro project
2) Copy enum.vm, fixed.vm, protocol.vm, record.vm into a /avro_templates directory off the main root of my project
3) Add the #com.fasterxml.jackson.annotation.JsonIgnore property to the template:
#end
#com.fasterxml.jackson.annotation.JsonIgnore
public org.apache.avro.specific.SpecificData getSpecificData() { return MODEL$; }
#com.fasterxml.jackson.annotation.JsonIgnore
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
// Used by DatumWriter. Applications should not call.
4) Update the gradle task:
avro {
dateTimeLogicalType="JSR310"
templateDirectory = "avro_templates/"
}
5) re-build avro classes
(everything now works)
I have generated a sample Multi-page Editor through the Eclipse wizard. Then, I have modified the sample plugin in order to have two pages:
Text Editor
Master Details Block
For the Master Details Block, I have used this tutorial.
I can open the Master Details Block page and I am also able to view the initialized objects in the list and display the corresponding details page. Now, I want to replace the static object with entries from the loaded file. My problem is, that I don't know how I can parse these entries from the text file. Do I need to implement my own parser, including the file handling or is this already implemented through a IFileEditorInput interface?
In my ScrolledPropertiesBlock class, I call the method viewer.setContentProvider(new MasterContentProvider());. I am sure that I need to modify the MasterContentProvider class implementation. So far, I have this:
class MasterContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if (inputElement instanceof FileEditorInput) {
//MyEditorInput input = (MyEditorInput) inputElement;
MyEditorInput input = new MyEditorInput("test");
return input.getModel().getContents();
}
return new Object[0];
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
If I delete the line MyEditorInput input = new MyEditorInput("test"); and do my cast instead, I get the following exception:
java.lang.ClassCastException: org.eclipse.ui.part.FileEditorInput cannot be cast to my.plugin.editor.MyEditorInput
Do I need to have a MyEditorInput which extends FormEditorInput (like in the example) and then implements IFileEditorInput?
public class MyEditorInput extends FormEditorInput {
private SimpleModel model;
public MyEditorInput(String name) {
super(name);
model = new SimpleModel();
}
public SimpleModel getModel() {
return model;
}
}
public class FormEditorInput implements IEditorInput {
private String name;
public FormEditorInput(String name) {
this.name = name;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#exists()
*/
public boolean exists() {
return true;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getImageDescriptor()
*/
public ImageDescriptor getImageDescriptor() {
return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
ISharedImages.IMG_OBJ_ELEMENT);
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getName()
*/
public String getName() {
return name;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getPersistable()
*/
public IPersistableElement getPersistable() {
return null;
}
/*
* (non-Javadoc)
*
* #see org.eclipse.ui.IEditorInput#getToolTipText()
*/
public String getToolTipText() {
return getName();
}
/*
* (non-Javadoc)
*
* #see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
*/
public Object getAdapter(Class adapter) {
return null;
}
}
The SimpleModel class looks like this:
public class SimpleModel {
private ArrayList modelListeners;
private ArrayList objects;
public SimpleModel() {
modelListeners = new ArrayList();
initialize();
}
public void addModelListener(IModelListener listener) {
if (!modelListeners.contains(listener))
modelListeners.add(listener);
}
public void removeModelListener(IModelListener listener) {
modelListeners.remove(listener);
}
public void fireModelChanged(Object[] objects, String type, String property) {
for (int i = 0; i < modelListeners.size(); i++) {
((IModelListener) modelListeners.get(i)).modelChanged(objects,
type, property);
}
}
public Object[] getContents() {
return objects.toArray();
}
private void initialize() {
objects = new ArrayList();
NamedObject[] objects = {
new TypeOne(Messages.getString("SimpleModel.t1_i1"), 2, true, Messages.getString("SimpleModel.text1")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i2"), 1, false, Messages.getString("SimpleModel.text2")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i3"), 3, true, Messages.getString("SimpleModel.text3")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i4"), 0, false, Messages.getString("SimpleModel.text4")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeOne(Messages.getString("SimpleModel.t1_i5"), 1, true, Messages.getString("SimpleModel.text5")), //$NON-NLS-1$ //$NON-NLS-2$
new TypeTwo(Messages.getString("SimpleModel.t2_i1"), false, true), //$NON-NLS-1$
new TypeTwo(Messages.getString("SimpleModel.t2_i2"), true, false)}; //$NON-NLS-1$
add(objects, false);
}
public void add(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.add(objs[i]);
objs[i].setModel(this);
}
if (notify)
fireModelChanged(objs, IModelListener.ADDED, ""); //$NON-NLS-1$
}
public void remove(NamedObject[] objs, boolean notify) {
for (int i = 0; i < objs.length; i++) {
objects.remove(objs[i]);
objs[i].setModel(null);
}
if (notify)
fireModelChanged(objs, IModelListener.REMOVED, ""); //$NON-NLS-1$
}
}
Eclipse gives you the input in an IEditorInput - this probably be an IFileEditorInput if the file is in the workspace. You can't cast this to something else. Creating new editor inputs does not help.
So you will have to read and parse the contents of the file from the editor input Eclipse gives you.
For a IFileEditorInput you can call getFile to get the input IFile. You can then call IFile.getContents to get an input stream containing the file's contents.