Hello jackson-user group,
I am trying to deserialize the following json string:String json = "{\"actions\":[[\"set-device-id\",[\"radius-avp\",\"nas-identifier\"]]]}";
into the object:
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
@JsonTypeName("set-device-id")
static public class SetDeviceId extends TransformAction {
public RadiusAvp radiusAvp;
public SetDeviceId() {}
...
}
Where TransformAction is this:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_ARRAY, property = "name")
@JsonSubTypes({@JsonSubTypes.Type(TransformAction.SetDeviceId.class),...)}
@JsonTypeName("transform-action")
public abstract class TransformAction {...}and RadiusAvp is:@JsonTypeName("radius-avp")
public class RadiusAvp {
public String attribute;
public RadiusAvp(String attribute) {
this.attribute = attribute;
}...}Doing this jackson errors:Unexpected token (VALUE_STRING), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class RadiusAvp at [Source:Is there any way of deserializing the provided json into SetDeviceId. I understand that Jackson doesn't seem to know about the RadiusAvp type since I provide no assignment to the SetDeviceId.radiusAvp field. Is there some Jackson annotation which is missing or wrong ?
@JsonTypeName("transform-rule")
public class TransformRule {
@JsonProperty("condition")
public TransformCondition condition;
@JsonProperty("actions")
public List<TransformAction> actions;
...
}
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
public class MyProblem {
ObjectMapper mapper;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_ARRAY, property = "name")
@JsonTypeName("radius-avp")
static public class RadiusAvp {
public String attribute;
public RadiusAvp() {
}
public RadiusAvp(String attribute) {
this.attribute = attribute;
}
public String toString() {
return "radius-avp = " + attribute;
}
}
@JsonTypeName("transform-rule")
static public class TransformRule {
public TransformRule() {
}
@JsonProperty("actions")
public List<SetDeviceId> actions;
public String toString() {
return "Transform-rule: " + actions.toString();
}
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_ARRAY, property = "name")
@JsonTypeName("set-device-id")
static public class SetDeviceId {
public RadiusAvp radiusAvp;
public SetDeviceId() {
}
// Uncomment this section to make this work (but we don't like this)
//
// @JsonCreator
// public SetDeviceId(Object object) {
// ObjectMapper mapper = new ObjectMapper();
// radiusAvp = mapper.convertValue(object, RadiusAvp.class);
// }
public String toString() {
return "set-device-id: " + radiusAvp.toString();
}
}
public static void main(String[] args) throws Exception {
String json = "{\"actions\":[[\"set-device-id\",[\"radius-avp\",\"nas-identifier\"]]]}";
ObjectMapper mapper = new ObjectMapper();
TransformRule transformRule = mapper.readValue(json, TransformRule.class);
System.out.println(transformRule.toString());
}
}
On Mon, Nov 11, 2019 at 7:13 PM Guido Rost <guid...@gmail.com> wrote:
>
>
> I just posted the upper level class which contains the actions.
> The target type is SetDeviceId which contains a field of type RadiusAvp. The construction of that is the actual problem
Yes, please do tell me what I really need.
-+ Tatu +-
>
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackso...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/35ebdc5e-8084-4347-8280-06bd42127877%40googlegroups.com.
By quick look, would it not work if you declared type of delegated thing as `RadiusAvp`, not Object?And if serialization needed, adding `@JsonValue` on that RadiusAvp field.-+ Tatu +-
To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/35ebdc5e-8084-4347-8280-06bd42127877%40googlegroups.com.
You're right, that works. Now it looks way better. But there is no way of Jackson creating SetDeviceId without the @JsonCreator annotation?
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/f3efba48-41d6-4455-980b-e9d75231ed85%40googlegroups.com.