How do I change a JSON array to JSON elements with Jackson - jackson

Can Jackson has a simpler way to deserialize a java object with a list into elements using Annotation?
Instead of this
{
[{id: 1, name: 'a'},{id: 2, name: 'b'}]
}
I want this
{
"1": {name: 'a'},
"2": {name: 'b'}
}

I used a custom serializer to do it
public static class CustomListSerializer extends StdSerializer<List> {
public CustomListSerializer() {
this(null);
}
public CustomListSerializer(Class<List> t) {
super(t);
}
#Override
public void serialize(
List list,
JsonGenerator generator,
SerializerProvider provider)
throws IOException, JsonProcessingException {
generator.writeStartObject();
int index = 1;
for (Object item : list) {
generator.writeObjectField(String.valueOf(index), item);
index++;
}
generator.writeEndObject();
}
}

Related

How to return ObjectNode from Controller in Micronaut?

I have following code
#Controller()
public class TestController {
#Get(value = "test", produces = MediaType.APPLICATION_JSON)
public MyDto fetch() throws Exception {
return new MyDto(
"test",
new ObjectMapper().readValue("{\"a\": 1}", ObjectNode.class)
);
}
#Serializable
#Data
public static class MyDto {
private final String name;
private final ObjectNode extraFields;
public MyDto(String name, ObjectNode extraFields) {
this.name = name;
this.extraFields = extraFields;
}
}
}
And I have an unexpected output on the client, extraFields object is empty
{
"name": "test",
"extraFields": [
[]
]
}
How to make Micronaut controller properly serialize com.fasterxml.jackson.databind.node.ObjectNode ?

Xunit Parameterize Selenium By Type

I have asked several basic questions related to this in the past and got great answers that explained several issues. I think i'm now in a position to ask the correct question now that I'm more aware of how Xunit works!
I am trying to parametrize several tests in C# using visual studio. I need each parameter to be displayed as an individual test that can be ran in isolation if required (I know there is a test collection runner and a separate test runner). The test collection runner is my issue.
I know that Xunit requires the parameters to be serialized in order for them to be picked up by the test collection runner. I also know that it by default can easily serialize basic data types like string, bool, int etc.
I have tried various approaches to do this with mixed results. My issue is trying to parameterize the Selenium type 'By'. I can't seem to be able to serialize this. I've tried to trick Xunit for example by using a dictionary List<string, By> and trying to serialize the in the dictionary (no luck!)
Here is the cleanest code I have come across that is simple and elegant for what i'm trying to do, but again I can't serialize the 'By' type. I have played around with changing the static property from bool to By and it returns only 1 test for all params, so it's not being serialized
public class ParamTest1
{
static string test3 = "TestXYZ";
public static TheoryData<int, bool, string, string> DataForTest1 = new TheoryData<int, bool, string, string>
{
{ 1, true, "First", test3 },
{ 2, false, "Second", test3},
{ 3, true, "Third", test3}
};
[Theory(DisplayName = "My First Test"), MemberData(nameof(DataForTest1))]
public void Test1(int valA, bool valB, string valC, string valD)
{
Assert.True(valB);
}
}
Which gives me
I am aware this particular code isn't invoking the IXunitSerializable
So here is an another working example of what I need but I just can't get it to work with the 'By' Type
public class ValidateTestCase : IXunitSerializable
{
public Guid Coupon { get; set; }
public bool IsValid { get; set; }
public void Serialize(IXunitSerializationInfo info)
{
info.AddValue(nameof(Coupon), Coupon.ToString());
}
public void Deserialize(IXunitSerializationInfo info) { }
}
public class Testing
{
public static IEnumerable<object[]> ValidateTestCases
{
get
{
yield return new object[] { new ValidateTestCase { Coupon = Guid.Parse("73e4d185-70cf-4ce4-bc3f-187b7a40e167"), IsValid = false } };
yield return new object[] { new ValidateTestCase { Coupon = Guid.Parse("93b983fb-5b6a-4845-a769-db41900b7df9"), IsValid = false } };
yield return new object[] { new ValidateTestCase { Coupon = Guid.Parse("99c03283-33cb-4e56-a010-c2bc0758ad27"), IsValid = false } };
yield return new object[] { new ValidateTestCase { Coupon = Guid.Parse("16a7fe80-3111-44b0-9ebf-c7159bea637d"), IsValid = false } };
yield return new object[] { new ValidateTestCase { Coupon = Guid.Parse("8b38b4aa-d70f-4ce7-8992-8a60936c5c58"), IsValid = false } };
yield return new object[] { new ValidateTestCase { Coupon = Guid.Parse("abc60aa0-a33b-4057-8f99-5cdceda35c70"), IsValid = true } };
}
}
[Theory(DisplayName = "CouponService should validate coupons")]
[MemberData(nameof(ValidateTestCases))]
public void MyCouponService_Validates(ValidateTestCase vtc)
{
Assert.Equal(vtc.IsValid, true);
}
}
And finally for anyone wondering what the 'By' type is I am referring to it's :
[![enter image description here][2]][2]
Here it is in the debugger so you can see what's going on inside:
[![enter image description here][3]][3]
I know there's a lot going on in there but if anyone has any ideas or suggestions it would be great!
To summarize, I can't parameterize the Selenium 'data type' By.
[2]: https://i.stack.imgur.com/XcLcn.png
[3]: https://i.stack.imgur.com/T9so1.png
Serializing Class
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;
using Xunit.Abstractions;
namespace XUnitTestProject1
{
public class ParameterizedHook
{
public By p1 { get; set; }
public By p2 { get; set; }
public string assertion { get; set; }
}
public class TheoryWrapper<T> : IXunitSerializable
{
public TheoryWrapper(string label, T #object)
{
Name = label;
Object = #object;
}
public TheoryWrapper()
{
}
public string Name { get; set; }
public T Object { get; set; }
public void Deserialize(IXunitSerializationInfo info)
{
Name = info.GetValue<string>("Label");
Object = JsonConvert.DeserializeObject<T>(info.GetValue<string>("objValue"));
}
public void Serialize(IXunitSerializationInfo info)
{
info.AddValue("Label", Name, typeof(string));
var json = JsonConvert.SerializeObject(Object);
info.AddValue("objValue", json);
}
public override string ToString()
{
return Name;
}
}
}
Working Test
public static IEnumerable<object[]> ComplexTheoryData
{
get
{
return new List<object[]>
{
new object[] {0, new TheoryWrapper<ParameterizedHook>("Pass0", new ParameterizedHook { p1 = nav_hold.pip_builder.dash_expand_pip_builder_menu, p2 = nav_hold.pip_builder.dash_project_stages, assertion = "WORK STAGE" })},
new object[] {0, new TheoryWrapper<ParameterizedHook>("Pass0", new ParameterizedHook { p1 = nav_hold.pip_builder.dash_expand_pip_builder_menu, p2 = nav_hold.pip_builder.dash_project_stages, assertion = "WORK STAGES" })},
//new object[] {0, new TheoryWrapper<ParameterizedHook>("Pass0", new ParameterizedHook { TestData = b.login })},
//new object[] {1, new TheoryWrapper<ParameterizedHook>("Pass1", new ParameterizedHook { TestData = b.password })}
};
}
}
[SkippableTheory]
[Trait("xUnit", "ForTestRunner")]
[MemberData(nameof(ComplexTheoryData))]
public void Test_Navigation(int id, TheoryWrapper<ParameterizedHook> test)
{
nav_met.NavMethodTest(test.Object.p1, test.Object.p2);
By page_title = By.Id("ctl00_lblPageTitle");
Assert.True(nav_met.VerifyText(page_title, test.Object.assertion));
}

How to send Collection of abstract types with ActionResult

I am trying to send back a collection of abstract types in a Controller using ActionResult.I do not know how to tell the serializer to also include derived type(s) specific properties:
public abstract class Base
{
public int Id{get;set;}
}
public class D1:Base
{
public string D1Value{get;set;}
}
public class D2:Base
{
public bool IsD2Value{get;set;}
}
public async Task<ActionResult<IEnumerable<Base>>> GetAll()
{
var collection=new []{ new D1 { Id=1, D1Value="hi"} ,new D2 {Id=2, IsD2Value=true}};
return StatusCode(200,collection);
}
How can i reach this result in a easy and elegant way.I have checked the JsonSerializer options but in my case i am not the one that is doing the serialization.
What i get
[{ "Id":1} , { "Id":2 }]
What i want
[{ "Id":1,"D1Value":"hi" } , { "Id":2 , "IsD2Value":true }]
Try the following code:
public async Task<ActionResult<IEnumerable<Base>>> GetAll()
{
var collection = new List<object>()
{
new D1 { Id = 1, D1Value = "hi" },
new D2 { Id = 2, IsD2Value = true }
};
return StatusCode(200, collection);
}
Here is the test result:
Use new ArrayList() instead of List.

How to serialize top level objects with Json.NET as objects and nested objects as references

I want to be able to serialize an object fully if it is at the top level of the serialization context but serialize objects lower in the context by reference.
I've searched and tried tests with Custom Contract Resolvers, Custom Json Converters and custom IReferenceResolver but I can't find a way to do this.
For example, imagine an IdType class that at the top level I want to serialize with all its properties but where I come across a reference to such an object in a property or list or dictionary I want to produce a reference.
For this type and test
public class IdType
{
public IdType(string id)
{
Id = id;
}
public string Id {get;}
public string Name {get;set;}
public int Number {get; set;}
public IdType OtherType { get; set; }
public IEnumerable<IdType> Types { get; set;}
public IDictionary<IdType, string> { get; set; }
public IDictionary<string, IdType> {get; set; }
}
[TestMethod]
public void SerializeTest()
{
var t1 = new IdType(1) { Name = 'Alice', Number = 42 };
var t2 = new IdType(2) { Name = 'Bob', Number = 21, OtherType = t1 };
var t3 = new IdType(2) { Name = 'Charlie', Number = 84, OtherType = t2, Types = new[] {t1, t2} };
var testTypes = new[]
{
t1,
t3
};
var serializer = new JsonSerializer
{
Formatting = Formatting.Indented,
};
StringWriter writer;
using (writer = new StringWriter())
{
serializer.Serialize(writer, myObject);
}
Console.WriteLine(writer.ToString());
}
I want output like this
[
{
"Id": "1",
"Name": "Alice"
"Number": 42,
},
{
"Id": "3",
"Name": "Charlie"
"Number": 84,
"OtherType": 2
"Types": [
"Id" : 1, 2
]
}
]
A JsonConverter has no context so it'll either always convert one way or another.
A custom resolver (derived from DefaultContractResolver) will work for a property of type IdType but I can't work out how to make it work with lists and dictionaries.
Latterly I've tried using PreserveReferenceHandling and a custom IReferenceResolver that has the IDs of the top level elements. But this doesn't work because the serialization is depth first.
Any suggestions to achieve this would be gratefully received
I think I've answered my own question. If I use a combination of a custom contract resolver and a custom converter and conditionally add the converter to the properties I want to serialize to IDs then it seems to work.
I've not implemented dictionaries yet but this works for basic properties and lists:
public class CustomResolver : DefaultContractResolver
{
readonly CamelCasePropertyNamesContractResolver innerResolver = new CamelCasePropertyNamesContractResolver();
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jsonProperty = base.CreateProperty(member, memberSerialization);
if (!jsonProperty.PropertyType.IsPrimitive && jsonProperty.PropertyType != typeof(string) && jsonProperty.Readable)
{
var innerContract = innerResolver.ResolveContract(jsonProperty.PropertyType);
if (typeof(IdType).IsAssignableFrom(innerContract.UnderlyingType) && innerContract is JsonObjectContract objectContract)
{
jsonProperty.Converter = new IdConverter();
}
else if (typeof(IEnumerable<IdType>).IsAssignableFrom(innerContract.UnderlyingType) && innerContract is JsonArrayContract arrayContract)
{
jsonProperty.Converter = new IdConverter();
}
}
return jsonProperty;
}
}
internal class IdConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (typeof(IdType).IsAssignableFrom(objectType) ||
typeof(IEnumerable<IdType>).IsAssignableFrom(objectType));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value is IdType item)
{
JToken token = JToken.FromObject(item.Id);
token.WriteTo(writer);
}
else if (value is IEnumerable<IdType> itemCollection)
{
JArray array = new JArray();
foreach (var i in itemCollection)
{
JToken token = JToken.FromObject(i.Id);
array.Add(token);
}
array.WriteTo(writer);
}
}
public override bool CanRead
{
get { return false; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
To serialize and making use of the custom resolver you would:
var serializer = new JsonSerializer
{
Formatting = Formatting.Indented,
ContractResolver = new CustomResolver(),
};

FasterXML schema generation for Maps and Object references

I have an issue generating a JSON Schema file with FasterXML.
The file output just shows
object type for a Map<String, String>
null type for OtherBean
{
"type": "object",
"properties": {
"beanId": {
"type": "integer"
},
"beanName": {
"type": "string"
},
"beanMap": {
"type": "object"
},
"otherBean": null
} }
My Schema generation class
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsonschema.JsonSchema;
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper MAPPER = new ObjectMapper();
JsonSchema jsonSchema = MAPPER.generateJsonSchema(MyBean.class);
MAPPER.writeValue(new File("MyBeanSchema.json"), jsonSchema);
}
}
MyBeans:
import java.util.Map;
public class MyBean {
private Integer beanId;
private String beanName;
private Map<String, String> beanMap;
private OtherBean otherBean;
public MyBean() {
}
public Integer getBeanId() {
return beanId;
}
public void setBeanId(Integer beanId) {
this.beanId = beanId;
}
public String getBeanName() {
return beanName;
}
public void setBeanName(String beanName) {
this.beanName = beanName;
}
public Map<String, String> getBeanMap() {
return beanMap;
}
public void setBeanMap(Map<String, String> beanMap) {
this.beanMap = beanMap;
}
public OtherBean getOtherBean() {
return otherBean;
}
public void setOtherBean(OtherBean otherBean) {
this.otherBean = otherBean;
}
}
OtherBean:
public class OtherBean {
}
Not directly answering your question, but Schema Generation is moving to a separate module:
https://github.com/FasterXML/jackson-module-jsonSchema/
which will have better functionality, and can evolve faster than old in-built generation.
So if possible, try using that. And then you can file bugs against this, for problems with generation.