create a Dynamic mapping for parsing multiple input files to an obj in kotlin

63
February 12, 2019, at 10:50 PM

Let's say I have a json data that can have different Key names for the same data field

Json type A

{ "width": 1024, "height": 768, "color": "#ff0000" }

also there is a mapping json that give me what attirbute should map with what in the data class Json type A

{ "width": w, "height": h, "color": "Color" }

Json Type B

{ "widthOfobj": 1024, "heightOfobj": 768, "colorOfobj": "#ff0000" }

Json Type mapping document B

{ "widthOfobj": w, "heightOfobj": h, "colorOfobj": "Color" }

in my kotlin data class I have obj defined as

data class dimension(W: int, H: int, Color: String)

now I want to map either of json type A or B to the kotlin data class with help of reading the mapping document. How would i go about doing something like that?

here is the flow I am thinking about for clarity purpose

sudo code

 Data class dimmenison( W: int, H: int, Color:String)// already defined
//Read mapping document to map 
    val Map = readmappingfile(mappingfile)  //e. g  Width to W
                                      //      Height to H
Val input Data= readJson(input) //      { "width": 1024, "height": 768, "color": "#ff0000" }
Val show result = applytransformation( mappingfile, input)
// inside applytransformation Read the Map and data and serialize it to data class  
//oneway of just serializing  it would be using gson 
val result = gson.fromJson(input,dimmension::class.java) // this would work if input was in format ( w->v , h-> v , Color->v) 

// how do i make that change to convert weight to W before i call gson code above to serialize it to data class. Note it needs to be dynamic enough so we dont Write up a hard coded mapping.

Answer 1

I think of you to use custom deserializer for gson.

check this example.

READ ALSO
How can I split string by a special character and ignore everything inside parentheses?

How can I split string by a special character and ignore everything inside parentheses?

I want to split the string by "/" and ignore "/" inside the outer parentheses

80
Comparing data in hql and return boolean value [on hold]

Comparing data in hql and return boolean value [on hold]

I want to compare data in hql and get boolean values to username variable, below some of my codes

81
Bad request only on safari browser

Bad request only on safari browser

I have a problem that I do not understand, on this site, in https and safari I have a bad request, on other browsers it's ok

62