Nested Json Serialise with Custom Class - flutter

I have a JSON data from the server from which I want to add specific JSON data to another class but maintaining its instance in the parent class
Json data from server:
{
"id:"1,
"name":"mike",
"classname:"STD-4"
}
#JsonSerialize
class Student{
#JsonKey(name:"id")
int id;
StudentInfo studentInfo;
}
#JsonSerialize
class StudentInfo{
#JsonKey(name:"name")
String name;
#JsonKey(name:"classname")
String classname;
}
Is this possible with JsonSerialize lib in flutter?

Yes, it should work fine. I assume you are using this package - https://pub.dev/packages/json_serializable?
There is also a parameter called explicitToJson, read more about it at https://pub.dev/documentation/json_serializable/latest/type_helper/ClassConfig/explicitToJson.html.

#JsonSerializable(explicitToJson: true)
class Student{
#JsonKey(name:"id")
int id;
StudentInfo studentInfo;
}
#JsonSerializable(explicitToJson: true)
class StudentInfo{
#JsonKey(name:"name")
String name;
#JsonKey(name:"classname")
String classname;
}
#JsonSerializable(explicitToJson: true)

Related

Cannot init the #FormBean field type as List<Integer> or Integer[]

import javax.ws.rs.FormParam;
import com.alibaba.fastjson.JSON;
Hi guys, The JAX-RS seems cannot init the list field in resteasy 2.x/3.x/4.x automatically,
Here is my VO used in servce
public class OrderVO {
#FormParam("id")
private long id;
#FormParam("title")
private String title;
#FormParam("product")
private List<Integer> product;
...
public String toString() {
return JSON.toJSONString(this);
}
}
And here is my service method:
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED+";charset=UTF-8")
#Path("/create")
public InsertResult saveOrder(#BeanParam OrderVO order) {
logger.debug(order.toString());
}
Other parameters are auto inited except the List or Integer[], and can anyone tell me why....

How to handle interface from graphql schema in java pojo

I have to create pojos from graphql schema file into java. like for
type Student{
name:String
age:Int
}
i created pojo
class Student{
private String name;
private int age;
// setter and getters
}
how to handle interface in java classes like
interface prop{
alttext: String
description: String
linkurl: String
title: String
}
I am writing a work around for my question, might be best solution is some thing else-
i created abstract class in java from interface of graphql schema. like below -
public abstract class prop {
private String alttext;
private String description;
private String linkurl;
private String title;
}
and for class extending this abstract class use super in constructor in child class to initialized abstract class members.

Converting JSON reponse to Java Object using REST API

I want to convert JSON to java
my json
var eateryRatingFactory = function() {
var self = this;
self.dineInMenu = [{
header: "Food",
items: [{
name: "Quality",
id: '1',
rating: '0'
},.....so on ]
},......so on ];
self.deliveryMenu = //some values same as beloew
};
my java class
public class EateryRatingAdapter {
public List<Ratings> dineInMenu;
public List<Ratings> deliveryMenu;
//setters and getters
public class Ratings{
public String header;
public List<Item> items;
//setters and getters
public class Item{
public String name;
public String id;
public String rating;
//setters and getters
}
}
}
while i'm getting the JSON at Request body , it is giving 400 bad request mean syntax mistake can u guys help this out
Use Object Mapper
ObjectMapper mapper = new ObjectMapper();
// Your JSON should cater your class
String jsonString = "{"header": "Food" , ................}";
//JSON from String to Object
**Ratings obj = mapper.readValue(jsonString, Ratings.class)**

Validating a Spring ResourceSupport-ed parent resource as a not empty property in a child resource

I'm looking for guidelines into validating a parent admin resource (AdminResource extending the Spring ResourceSupport class) as not being empty (#NotEmpty) in a child admin module resource (AdminModuleResource extending the Spring ResourceSupport class).
I understand the AdminResource class should also implement the Serializable interface ? Is that the way to go with Spring ResourceSupport-ed resources ?
Here are my resources:
public class AdminResource extends AbstractResource {
private String firstname;
private String lastname;
#NotEmpty
#Email
private String email;
private String password;
private String passwordSalt;
}
public class AdminModuleResource extends AbstractResource {
#NotEmpty
private String module;
#NotEmpty
private AdminResource adminResource;
}
public abstract class AbstractResource extends ResourceSupport {
#JsonProperty("id")
private Long resourceId;
public AbstractResource() {
}
public Long getResourceId() {
return resourceId;
}
public void setResourceId(Long resourceId) {
this.resourceId = resourceId;
}
}
As of now, the #NotEmpty validator annotation gives me the error: No validator could be found for type...
But adding the "implements Serializable" to the resources did not help and the exception remained when using the #NotEmpty validator annotation.
public abstract class AbstractResource extends ResourceSupport implements Serializable {
}
Of course, commenting out the #NotEmpty validator annotation makes the Maven build successful.
Thanks for any directions tips !
Kind Regards,
Stephane
#NotEmpty is only supported for CharSequences (String), Collections, Maps and arrays. It either checks whether the string or collection/array is empty. What does it even mean that a AdminResource is not empty. Do you mean #NotNull?
If it really would make semantically sense to have a #NotEmpty for AdminResource, you would have to implement a custom ConstraintValidator for it and registering it via XML (see also http://beanvalidation.org/1.1/spec/#xml-mapping-constraintdefinition).

TomEE Resteasy JAX-B -> Can not get Nested Object

I'm working on a RestWebService using Resteasy. The basic implementation works fine. Know I tried to return a Complexer- Object through rest...
Actually its pretty easy..I thought. I'm getting a problem because of my nested object (Address)...
What I try is this:
#XmlRootElement(name = "person")
#XmlAccessorType(XmlAccessType.FIELD)
public class Person implements Serializable {
private static final long serialVersionUID = 1199647317278849602L;
private String uri;
private String vName;
private String nName;
private Address address;
.....
#XmlElementWrapper(name="Former-User-Ids")
#XmlElement(name="Adress")
public Address getAddress() {
return address;
}
....
Address looks like this:
#XmlRootElement(name = "address")
#XmlAccessorType(XmlAccessType.FIELD)
public class Address {
private String uri;
private String street;
private String city;
public String getCity() {
return city;
}
public String getStreet() {
return street;
}
....
The Restservice looks like this. It worked perfect without the address object..
#Path("/getPersonXML/{personNumber}")
#GET
#Produces(MediaType.APPLICATION_XML)
public Patient getPatientXML(#PathParam("personNumber") String personNumber) throws ParseException {
Address a1 = new Address("de.person/address/" + "432432","Teststret12","TestCity", "32433", "TestCountry", "081511833");
Patient p1 = new Person();
p1.setAddress(a1);
p1.setUri("de.spironto/person/"+ "432432");
p1.setnName("Power");
p1.setvName("Max");
return p1;
}
At the moment I'm always getting a
javax.xml.bind.JAXBException:
Any Ideas?
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
PROBLEM
The #XmlElementWrapper annotation must be used with a collection property. This means you can have:
#XmlElementWrapper
public List<PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
But not
#XmlElementWrapper
public Address getAddress() {
return address;
}
SOLUTION #1 - Using Any JAXB Proivder
You could use an XmlAdapter to accomplish this (see linked answer below):
Access attribute of internal element in the most simple way
SOLUTION #2 - Using EclipseLink JAXB (MOXy)
You could leverage the #XmlPath extension to map this use case:
#XmlPath("Former-User-Ids/Address")
public Address getAddress() {
return address;
}
For More Information
http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
After building a small marshaller test. I got the failure that there are several properties with the same name. So I tried to delete all #XML_Eleemets annotations in the Address class.
That worked for me...