How to deserialize Yaml to POJO when a key is dynamic? I have a Yaml file with this structure:
paths
/user:
get:
summary: Example summary
description: Some description
/account
post:
summary: Another summary
description: Another description
And I need to deserialize it to a Java Object:
public class PathsWrapper {
private List<Path> paths;
}
public class Path {
private String url;
private List<Method> methods;
}
public class Method {
private String method;
private String summary;
private String description;
}
You can use a map like so:
public class PathsWrapper {
private List<Path> paths;
}
public class Path {
private Map<String,List<Method>> url;
}
public class Method {
private Map<String, MethodDescription> method;
}
public class MethodDescription {
private String summary;
private String description;
}
Related
I'm getting error when spring mongo template reading object from DB: "Class is abstract". This is because internal field in document is abstract type.
in my case classes looks like:
public abstract class Context {
private String name;
}
public class AContext {
private String aData;
}
public class BContext {
private String bData;
}
#Document
#TypeAlias("Task")
public class Task {
#Id
private String id;
private String name;
private List<Context> contexts;
}
How can I fix thiss issue ?
#Reports
reports:
PnLReport:
reportId: 10
path: \\\\pathto\\PnLreport\\
BalanceSheetReport:
reportId: 11
path: \\\\pathto\\balancesheet\\
schedule-10:
description: Deliver pnl reports
report: 10
format: PDF, XLS
I have the above properties defined in aapplication.yml file in my Spring Boot application.
How can I map the repordId and path properties to a enum for example for each of the reports types. For example:
public enum ReportType{
PNL(...)
BALANCE(...);
private final String reportId;
private final String path;
private ReportType(String reportId, String path) {
this.identifier = identifier;
}
Next, I would like map between report: 10 under the schedule-10 property to the reportId to derive the path in a FileService class for example so that I can look if the files exist in the path. How can I do this mapping?
This is the only way I can think of for my requirement, is there a better approach to this?
I'm not sure I would recommend using an enumeration for this since what you're looking for is more like a configurable property instance. Maybe consider using a simple class instead and read two instances of it?
public class ReportType {
private Integer reportId;
private String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public void getReportId() {
return reportId;
}
public Integer setReportId(Integer reportId) {
this.reportId = reportId;
}
}
#Component
#ConfigurationProperties(prefix = "reports")
public class ReportTypes {
public ReportType PlnReport;
public ReportType BalanceSheetReport;
}
I have below json string :-
{"name":"Test","sortlist":[],"filterlist":[{"fieldname":"regions_id","operator":"equals","value":{"id":1,"code":"HIGH","description":"HIGH Region","comment":"High Region","active":true}}]}
and Java class as below :-
#JsonSerialize
#JsonDeserialize
public class ItemFilter implements Serializable {
private String name;
private List<FieldFilter> filterlist = new ArrayList<FieldFilter>();
}
public class FieldFilter implements Serializable {
private static final long serialVersionUID = 1L;
private String fieldname;
private String operator;
private Object value;
}
and my convert method as below :-
public static ItemFilter convertItemFilter(String item) {
ObjectMapper mapper = new ObjectMapper();
try {
ItemFilter itemFilter = mapper.readValue(item, new TypeReference<ItemFilter>(){});
return itemFilter;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
ItemFilter domain is getting converted correctly but in private Object value; field i am getting LinkedHashMap i want to get an simple object and later i will type cast it.
Can someone please guide me how to escape LinkedHashMap and get an simple Java Object in variable?
i cant use hard coding Object type because its a generic pojo which can have any object type. hard coding will make this pojo very bigger and frontend also need to change for it. So that why i have used Object as data type.
The following class structure should return the JSON to "YourObject"
public class YourObject{
private String name;
private List<String> sortList;
private List<Filter> filterList;
public static class Filter{
private String fieldname;
private String operator;
private Value value;
}
public static class Value{
private Integer id;
private String code;
private String description;
private String comment;
private Boolean active;
}
}
Then use the following to read it into the object:
YourObject itemFilter = mapper.readValue(item, YourObject.class);
I have the following xml
<MyPojo>
<name>Jason</name>
<age>25</age>
<meta>
<occupation>Engineer</occupation>
</meta>
</MyPojo>
I need to deserialize it to the following POJO:
public class MyPojo {
private String name;
private int age;
private String occupation;
}
The problem here is that occupation is wrapped within meta element
You need one more object:
public class MyPojo {
private String name;
private int age;
private Meta meta;
}
public class Meta{
private String occupation;
}
My idea is to replace occupation with an own class. Something like myMeta or whatever you want to call it(be aware in your case like the xml says: meta). This class should cotain the field occupation:
public class Meta
{
private String occupation;
}
After that you only have to add a new field of your new class e.g. myMeta to myPojo. Something like this:
public class MyPojo
{
private String name;
private int age;
private Meta meta;
}
this should avoid
that occupation is wrapped within meta element
Hope that helps!
I am new to the simple xml library. I really like it but I have got a problem.
Here are my classes (some code has been removed to make it more concise):
#Root
#ElementList
public class MyArrayList<E> extends ArrayList<E>{
public void ToXml() throws Exception{
Serializer serializer = new Persister();
File file = new File("somewhere in my file system");
serializer.write(this, file);
}
}
¬
#Root
public abstract class MediaEntry implements Serializable {
private static final long serialVersionUID = 1L;
#Element
public String Title;
#Element
public String Description;
#Element
public String Url;
#Element
public String LocalPath;
public MediaEntry(String title, String description,
String url, String localPath) {
Title= title;
Description= description;
Url= url;
LocalPath= localPath;
}
}
¬
public class VideoEntry extends MediaEntry {
private static final long serialVersionUID = 1L;
public VideoEntry(String title, String description,
String url, String localPath) {
super(title, description, url, localPath);
}
}
When I instantiate MyArrayList add some VideoEntries and call ToXml, I only get an empty root ie.
<MyArrayList />
How to I solve this? Is it something to do with the fact that MyArrayList is generic?
The List has to be a member of an element (and no seperate class) to get the desired behaviour, you can set the ElementList inline, so there is no parent element.
#Root
public class MyArrayList<E> {
#ElementList(inline=true)
ArrayList<E> list = new ArrayList<E>();
public boolean add(E entry) {
return list.add(entry);
}
public void ToXml() throws Exception {
Serializer serializer = new Persister();
File file = new File("somewhere in my file system");
serializer.write(this, file);
}
}
Just thought of another solution which may even be better (you have access to all the List functions - but I'm not sure if there are any side-effects, so i leave my original solution)
#Root
public class MyArrayList<E> extends ArrayList<E> {
#ElementList(inline=true)
MyArrayList<E> list = this;
public void ToXml() throws Exception {
Serializer serializer = new Persister();
File file = new File("somewhere in my file system");
serializer.write(this, file);
}
}
To deserialize you have to declare for SimpleXML, whicht element is used for which constructor-parameter:
#Root
public abstract class MediaEntry implements Serializable {
private static final long serialVersionUID = 1L;
#Element
public String Title;
#Element
public String Description;
#Element
public String Url;
#Element
public String LocalPath;
public MediaEntry(#Element(name = "Title") String title,
#Element(name = "Description") String description,
#Element(name = "Url") String url,
#Element(name = "LocalPath") String localPath) {
Title = title;
Description = description;
Url = url;
LocalPath = localPath;
}
}
Btw, if you are just start programming Java, you might consider reading the Java code conventions - it is not a good practice to start method- and variablenames with a capital letter (so you can prevent getting used to bad habits ;-))