ormlite store unknown foreign object in DatabaseField - java

For example :
i need an Animals table which will contain animal_type as Object or T type as foreign relation field.
then i will have 10 different tables like Dog/Cat/Horse etc.
at the time of insertion based on type of animal i would set animal_type of Animal object.
I don't want to take multiple databaseField for each type as a foreign relation, instead i need one generic field , Using ormlite version 5.0.
please refer this json:{
"animal": {
"type": 1,
"content": {
"items": [
{
"title": "Image",
"desc": "This is item with image",
"media": {
"url": "https://upload.wikimedia.org/wikipedia/commons/9/99/Earth_rise_from_the_Moon_AS11-44-6550_2.JPG",
"type": 0
},
"options": [
{
"title": "View",
"value": "view"
},
{
"title": "Download",
"value": "download"
}
],
"url": "https://commons.wikimedia.org/wiki/File%3AEarth_rise_from_the_Moon_AS11-44-6550_2.JPG"
}
]
}
}
}

Related

Plugin jsonschema2pojo: Properties are shown as required but they should be optional

i have a personschema which has e.g. those two fields:
"name": {
"title": "Last name",
"type": "string",
"minLength": 1,
"isRequired" : true
}
"birthday": {
"title": "Date of birth",
"$ref": "resource:schema/general/dateSchema.json",
"isRequired" : true
},
"birthCountry": {
"$ref": "resource:schema/general/countrySchema.json",
"title": "Country of birth",
"isRequired" : true
}
Then I have another schema which extends the person schema. In this schema, I want those two properties (birthCountry and birthday) to be optional and only the name to be mandatory. I've tried it like that:
{
"$schema": "http://json-schema.org/draft/2019-09/schema",
"title": "Other Schema",
"type": "object",
"javaType": "..json_schema_model.dto.OtherSchema",
"description": "Other Schema Description",
"extendsJavaClass": "..json_schema_model.dto.PersonSchema",
"allOf": [{
"required": [
"name"
],
"$ref": "resource:schema/general/personSchema.json"
}
But unfortunately, in the API-Docs they are still remarked as mandatory.
The issue was that properties can only be added in child classes and not modified, so I rearranged the class structure to modify the decision whether a value is mandatory will only be decided in the child classes themselves. Also please note, that sibling values alongsid $refs are ignored. To add properties to a $ref, you need to wrap it into an allOf.

Filter nested json data using jsonpath as in example

I am using jsonpath to filter.
Json(Dummy json just to explain) source String, which is basically a list of Operating systems and details of its programs etc. In this example, the OS whose id = 1403 is a windows 10 OS and has 2 features acchritecture and browser. There are more details to the browser feature as shown in json
[
{
"id": 1403,
"os": "window 10",
"features": [
{
"id": 1356,
"name": "architecture",
"value": [
{
"id": 1308,
"feature": [
{
"id": 1262,
"key": "name",
"value": "amd64"
}
]
}
],
"category": "cat1"
},
{
"id": 1357,
"name": "browser",
"value": [
{
"id": 1309,
"feature": [
{
"id": 1263,
"key": "name",
"value": "Firefox"
},
{
"id": 1265,
"key": "version",
"value": "187"
}
]
}
],
"category": "cat2"
}
]
},
{
"id": 2804,
"os": "window 7",
"features": [
{
"id": 2764,
"name": "architecture",
"value": [
{
"id": 2719,
"feature": [
{
"id": 2679,
"key": "name",
"value": "amd64"
}
]
}
],
"category": "cat1"
},
{
"id": 2765,
"name": "browser",
"value": [
{
"id": 2722,
"feature": [
{
"id": 2685,
"key": "name",
"value": "Chrome"
},
{
"id": 2684,
"key": "version",
"value": "87.0.4280.88"
}
]
}
],
"category": "cat2"
}
]
}
]
I want to be able to filter the json such that
features[*].name == 'browser' and features[*].value[*].feature[*].value == 'chrome'
What will be the JsonPath string that can help me achieve above query? The above query uses similar syntax used by JsonPath string but doesn't do the job. Its just to explain.
There is another example here gets Movie Title Given 'Starring' field
And would like to get the full OS json that fulfils this condition. In this case a array of OS which contains only one OS i.e. with id= 2804
[
{
"id": "2804",
...
}
]
I am stuck much before what aim to achieve. Here is my code to get all the OS that have "name=browser". I get the array but it only contains value[] items. I want it get the full json. It returns object with IDs- 1357, 2765.
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString)
.read("$[*].features[*].[?(#.name == 'browser')]");
To get the outer array you need to use the filter like $[?(...)]
For your current use case, we need to use nested array filters. There is an open issue in JsonPath for filter on children level. (Refer here).
Luckily, there is a workaround suggested to use contains over here.
we can use the below expression to filter:
List<Object> expensive = JsonPath.parse(jsonDataSourceString)
.read("$[?(#.features[?(#.name == 'browser')].value[*].feature[*].value contains 'Chrome')]");
Prints the below output
{id=2804, os=window 7, features=[{"id":2764,"name":"architecture","value":[{"id":2719,"feature":[{"id":2679,"key":"name","value":"amd64"}]}],"category":"cat1"},{"id":2765,"name":"browser","value":[{"id":2722,"feature":[{"id":2685,"key":"name","value":"Chrome"},{"id":2684,"key":"version","value":"87.0.4280.88"}]}],"category":"cat2"}]}

How to validate a field to have specific value only in AVRO Schema

I am using Avro Schema to validate my schema is:
{
"name": "Example",
"type": "record",
"fields": [
{
"name": "custId",
"type": "string"
},
{
"name": "danger",
"type": {
"type": "enum",
"symbols": [0,1,2,3],
"name": "danger"
}}
]
}
i have a requirement where i want field "danger" to have values 0,1,2,3 . If something else is given schema should not validate it. I know enum type are used for that but it allow only string.
How can i achieve this.

How can I get Avro SchemaBuilder to build a schema of a map between two 'records'?

Trying to serialize Java Objects represented as a Map. This requires me to build an Avro Schema first. I am using Avro SchemaBuilder for schema generation. Have already succeeded in schema generation for Map's objects. However can't deal with schema building for overall Map.
Final schema should look like the following (created this one manually):
{
"type": "record",
"name": "MapObject",
"namespace": "test",
"fields": [
{
"name": "CacheMap",
"type": {
"type": "map",
"values": [
{
"type": "record",
"name": "User",
"namespace": "test",
"fields": [
{
"name": "id",
"type": "long"
},
{
"name": "status",
"type": "string"
}
]
},
{
"type": "record",
"name": "UserKey",
"namespace": "test",
"fields": [
{
"name": "key",
"type": "long"
},
{
"name": "keyPrint",
"type": "string"
}
]
}
]
}
}
]
}
How can I pass to different records for key and value of a map in SchemaBuilder?
SchemaBuilder.map().values() does not allow to pass more than one Schema as a parameter for .value().
Apache Avro Unions can be of help here.
SchemaBuilder.builder()
.map()
.values(SchemaBuilder.unionOf().type(firstSchema).and().type(secondSchema).endUnion());
where the firstSchema and the secondSchema are separate schemas already defined for required User and UserKey 'records' (considering the schema from question)

Serialize JSON to Scala case class

I need to serialize JSON to scala case class. It is not a question about JSON serialization, but is a question about class mapping in scala.
The example of JSON:
{
"id": 98433,
"name": "Santa Cruz Bronson",
"vendor": {
"id": 344,
"name": "Santa Cruz"
},
"category": {
"id": 132,
"name": "Bicycles"
},
"annotation": "The best downhill cycle",
"description": "Rich text is here",
"classification": {
"id": 12,
"name": "138-cycles"
},
"properties": [{
"id": 84436,
"group": {
"id": 19433,
"name": "Suspension"
},
"name": "Fork turn",
"description": "Fork turn defines bike suspension",
"value": "129mm"
}, {
"id": 84436,
"group": {
"id": 19433,
"name": "Suspension"
},
"name": "Fork turn",
"description": "Fork turn defines bike suspension",
"value": "129mm"
}, {
"id": 84436,
"group": {
"id": 19433,
"name": "Suspension"
},
"name": "Fork turn",
"description": "Fork turn defines bike suspension",
"value": "129mm"
}, {
"id": 84436,
"group": {
"id": 19433,
"name": "Suspension"
},
"name": "Fork turn",
"description": "Fork turn defines bike suspension",
"value": "129mm"
}],
"isGroup": true
}
I know how to build a case class for top level map:
case class ProductDocument(id: Long, name: String, annotation: String, description: String, isGroup: String) extends DocumentMap {
...
}
But I have no idea how to build values for vendor, category, properties, etc.
I want to define map for this JSON into one class file.
Each of the nested json objects should be defined as their own case classes, such that:
case class Vendor(id: Long, name: String)
case class ProductDocument(id: Long, ..., vendor: Vendor)
The properties would become a List of a Property case class:
case class Property(id: Long, group: PropertyGroup, name: String, description: String, value: String)
case class PropertyGroup(id: Long, name: String)
case class ProductDocument(id: Long, ..., properties: List[Property])
This assumes that you are using json4s serialization.
For scala-json mapping you have at least two options:
Salat - which originally provided ORM-like functionality for Mongo but also works for just JSON serialization (disclaimer: I work at Novus)
json4s - see section "Serialization"
The good news is that because the structure of Scala case classes are well-understood, you generally do not have to define "mapping" between your JSON fields and case class fields. You just write your case classes to match the structure / fields of your JSON document, as Arne Claassen has sketched out.

Categories

Resources