JSON Object Property with space in Scala using Json4s(Jackson) - json

I have a JSON String in the given below form:
var jStr =
""" {
|"company":{
|"company name":"ABCD"
|},
|"person":[
|{"name":"john",
|"age":"28"
|},
|{
|"name":"AWQ",
|"age":"45"
|}
|]
|}
""".stripMargin
See the property "company name". Due to this I cannot extract it from its json form to its case class without changing the "company name" to "company_name"(or anything else). Below is the code:
import com.fasterxml.jackson.annotation.{JsonCreator, JsonProperty}
import org.json4s._
import org.json4s.native.JsonMethods._
val parseJson = parse(jStr)
var obj = parseJson.extract[Info] // Exception Here
case class Company(cname : String)
case class Info(company: Company,person: List[Person])
case class Person(name : String , age : String)
I tried using #JsonProperty and #JsonCreator but they too failed.
#JsonCreator
case class Company( #JsonProperty("company name") cname : String)
case class Info(company: Company, person: List[Person])
case class Person(name : String , age : String)
I need to map a json property having spaces to its respective case class(which obviously can't have a space!) using Json4s in Scala.
Stacktrace:
Exception in thread "main" org.json4s.package$MappingException: No usable value for company
No usable value for cname
Did not find value which can be converted into java.lang.String
at org.json4s.reflect.package$.fail(package.scala:95)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:548)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$3.applyOrElse(Extraction.scala:572)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$3.applyOrElse(Extraction.scala:570)
at scala.PartialFunction.$anonfun$runWith$1$adapted(PartialFunction.scala:145)
at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
at scala.collection.TraversableLike.collect(TraversableLike.scala:407)
at scala.collection.TraversableLike.collect$(TraversableLike.scala:405)
at scala.collection.AbstractTraversable.collect(Traversable.scala:108)
at org.json4s.Extraction$ClassInstanceBuilder.instantiate(Extraction.scala:570)
at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:630)
at org.json4s.Extraction$.$anonfun$extract$10(Extraction.scala:416)
at org.json4s.Extraction$.$anonfun$customOrElse$1(Extraction.scala:637)
at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)
at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)
at scala.PartialFunction$$anon$1.applyOrElse(PartialFunction.scala:257)
at org.json4s.Extraction$.customOrElse(Extraction.scala:637)
at org.json4s.Extraction$.extract(Extraction.scala:408)
at org.json4s.Extraction$.extract(Extraction.scala:40)
at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
at JsonTest.convertToJSON(JsonTest.scala:100)
at Main$.main(Main.scala:8)
at Main.main(Main.scala)
Caused by: org.json4s.package$MappingException: No usable value for cname
Did not find value which can be converted into java.lang.String
at org.json4s.reflect.package$.fail(package.scala:95)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:548)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$3.applyOrElse(Extraction.scala:572)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$3.applyOrElse(Extraction.scala:570)
at scala.PartialFunction.$anonfun$runWith$1$adapted(PartialFunction.scala:145)
at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
at scala.collection.TraversableLike.collect(TraversableLike.scala:407)
at scala.collection.TraversableLike.collect$(TraversableLike.scala:405)
at scala.collection.AbstractTraversable.collect(Traversable.scala:108)
at org.json4s.Extraction$ClassInstanceBuilder.instantiate(Extraction.scala:570)
at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:630)
at org.json4s.Extraction$.$anonfun$extract$10(Extraction.scala:416)
at org.json4s.Extraction$.$anonfun$customOrElse$1(Extraction.scala:637)
at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)
at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)
at scala.PartialFunction$$anon$1.applyOrElse(PartialFunction.scala:257)
at org.json4s.Extraction$.customOrElse(Extraction.scala:637)
at org.json4s.Extraction$.extract(Extraction.scala:408)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:534)
... 23 more
Caused by: org.json4s.package$MappingException: Did not find value which can be converted into java.lang.String
at org.json4s.reflect.package$.fail(package.scala:95)
at org.json4s.Extraction$.$anonfun$convert$2(Extraction.scala:735)
at scala.Option.getOrElse(Option.scala:189)
at org.json4s.Extraction$.convert(Extraction.scala:735)
at org.json4s.Extraction$.$anonfun$extract$10(Extraction.scala:410)
at org.json4s.Extraction$.$anonfun$customOrElse$1(Extraction.scala:637)
at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)
at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)
at scala.PartialFunction$$anon$1.applyOrElse(PartialFunction.scala:257)
at org.json4s.Extraction$.customOrElse(Extraction.scala:637)
at org.json4s.Extraction$.extract(Extraction.scala:408)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:534)
... 42 more
Process finished with exit code 1

Related

kotlinx.serialization.MissingFieldException: Field 'X' is required for type with serial name but it was missing Error in kotlin

I am making a dictionary application and I have my own json dataset inside the asset file. I read from this data set for the first time and save it to the room, and then I read from the room for other times. However, I am encountering this error.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.enestigli.dictionaryapp, PID: 1868
kotlinx.serialization.MissingFieldException: Field 'example' is required for type with serial name 'com.enestigli.dictionaryapp.data.locale.datamodel.Idioms', but it was missing
at kotlinx.serialization.internal.PluginExceptionsKt.throwMissingFieldException(PluginExceptions.kt:20)
at com.enestigli.dictionaryapp.data.locale.datamodel.Idioms.<init>(Idiom.kt:28)
at com.enestigli.dictionaryapp.data.locale.datamodel.Idioms$$serializer.deserialize(Idiom.kt:28)
at com.enestigli.dictionaryapp.data.locale.datamodel.Idioms$$serializer.deserialize(Idiom.kt:28)
at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59)
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36)
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
at kotlinx.serialization.encoding.CompositeDecoder$DefaultImpls.decodeSerializableElement$default(Decoding.kt:535)
at kotlinx.serialization.internal.ListLikeSerializer.readElement(CollectionSerializers.kt:80)
at kotlinx.serialization.internal.AbstractCollectionSerializer.readElement$default(CollectionSerializers.kt:51)
at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:36)
at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59)
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36)
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
at com.enestigli.dictionaryapp.data.locale.datamodel.IdiomItem$$serializer.deserialize(Idiom.kt:15)
at com.enestigli.dictionaryapp.data.locale.datamodel.IdiomItem$$serializer.deserialize(Idiom.kt:15)
at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59)
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36)
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
at kotlinx.serialization.encoding.CompositeDecoder$DefaultImpls.decodeSerializableElement$default(Decoding.kt:535)
at kotlinx.serialization.internal.ListLikeSerializer.readElement(CollectionSerializers.kt:80)
at kotlinx.serialization.internal.AbstractCollectionSerializer.readElement$default(CollectionSerializers.kt:51)
at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:36)
at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59)
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36)
at com.enestigli.dictionaryapp.data.locale.datamodel.IdiomDataModel$$serializer.deserialize-zhuhEdE(Idiom.kt:8)
at com.enestigli.dictionaryapp.data.locale.datamodel.IdiomDataModel$$serializer.deserialize(Idiom.kt:8)
at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59)
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:36)
at kotlinx.serialization.json.Json.decodeFromString(Json.kt:100)
at com.enestigli.dictionaryapp.data.locale.datasource.AssetDataSource.getIdioms-zhuhEdE(AssetsDataSource.kt:84)
E/AndroidRuntime: at com.enestigli.dictionaryapp.data.repository.IdiomsRepositoryImpl.initData(IdiomsRepositoryImpl.kt:21)
at com.enestigli.dictionaryapp.domain.use_case.idiom.insert.InsertIdiomsUseCase.initIdiomData(InsertIdiomsUseCase.kt:12)
at com.enestigli.dictionaryapp.presentation.SplashScreen.SplashScreenViewModel$insertAllDataToRoomDb$1.invokeSuspend(SplashScreenViewModel.kt:38)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}#5aeb15c, Dispatchers.Main.immediate]
my json dataset looks like this
[
{
"letter": "A",
"idioms": [
{
"idiom": "above board",
"meaning": "If something is above board, it's been done in a legal and honest way.",
"examples": [
"I'm sure the deal was completely above board as I know James well and he'd never do anything illegal or corrupt.",
"The minister claimed all the appointments were above board and denied claims that some positions had been given to his friends."
]
},
{
"idiom": "above the law",
"meaning": "If someone is above the law, they are not subject to the laws of a society.",
"examples": [
"Just because his father is a rich and powerful man, he seems to think he's above the law and he can do whatever he likes.",
"In a democracy, no-one is above the law - not even a president or a prime-minister."
]
},
{
"idiom": "Achilles' heel",
"meaning": "An Achilles' heel is a weakness that could result in failure.",
"examples": [
"He's a good golfer, but his Achilles' heel is his putting and it's often made him lose matches.",
"The country's dependence on imported oil could prove to be its Achilles' heel if prices keep on rising."
]
},
My Data Model
#JvmInline
#Serializable
value class IdiomDataModel(
val allData: List<IdiomItem>
)
#Serializable
data class IdiomItem(
val letter: String,
val idioms: List<Idioms>
) {
fun toEntity() = IdiomEntity(
letter = letter,
idioms = idioms
)
}
#Serializable
data class Idioms(
val idiom: String,
val meaning: String,
val example: List<String>
)
Entity
#Entity(tableName = "idioms")
data class IdiomEntity(
#ColumnInfo(name = "letter") val letter:String,
#ColumnInfo(name = "idioms") val idioms:List<Idioms>,
#PrimaryKey(autoGenerate = true) val uid:Int? = null
)
I think the val example: List<String> in the Idioms data class is from this part. Could it be that this does not exactly match the example list in the Json dataset?
In your Idioms data class, rename example to examples and it should work.
Old:
#Serializable
data class Idioms(
val idiom: String,
val meaning: String,
val example: List<String>
)
New
#Serializable
data class Idioms(
val idiom: String,
val meaning: String,
val examples: List<String>
)

How to convert Json objects into arrays [(string, double)] in Scala JSON4s?

I don't know how to use CustomSerializer to convert JSON to Scala data
I don't need to serialize the parts, but deserialization is difficult
Can someone help me? Thank you very much
{
"parameters": {
"field_owner": true,
"sample_field": "gender",
"sample_strategy": "by_ratio",
"sample_config": [["male", 1500.0], ["female", 1500.0]],
"with_replacement": true,
"random_seed": 114514,
"least_sample_num": 100
},
"input_artifacts": {"dataset": {"object_id": "upload/ml_classifier_predict_result"}},
"output_artifacts": {"predict_result": {"object_id": "ingest_output.csv"}}
}
class TupleSerializer extends CustomSerializer[(String,Double)](tuple_formats => (
{
case JArray(List(JString(x), JDouble(y))) =>
(x,y)
},{
case x:Tuple2[String,Double] => null
}
))
implicit val formats: Formats = DefaultFormats + new TupleSerializer
val fileBuffer = Source.fromFile(path)
val jsonString = fileBuffer.mkString
parse(jsonString).extract[StratifiedSampleArgument]
}
error message:
o usable value for parameters
No usable value for sample_config
Expected object with 1 element but got JArray(List(JString(male), JDouble(1500.0)))
at com.alipay.morse.sgxengine.driver.StratifiedSampleDriverTest.main(StratifiedSampleDriverTest.scala:15)
Caused by: org.json4s.package$MappingException:
No usable value for sample_config
Expected object with 1 element but got JArray(List(JString(male), JDouble(1500.0)))
at com.alipay.morse.sgxengine.driver.StratifiedSampleDriverTest.main(StratifiedSampleDriverTest.scala:15)
Caused by: org.json4s.package$MappingException: Expected object with 1 element but got JArray(List(JString(male), JDouble(1500.0)))
at com.alipay.morse.sgxengine.driver.StratifiedSampleDriverTest.main(StratifiedSampleDriverTest.scala:15)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] StratifiedSampleDriverTest.main:15 » Mapping No usable value for parameters
No...
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

how to convert from JSON to a parametric nested struct in julia?

stringtest="""{ "struct_A": {
"arg1": {
"arg_B": 90
},
"arg2": 100
}
}"""
jsonDict=JSON.parse(stringtest)
struct struct_B<: myStruct
arg
end
struct struct_A{T<:myStruct}
arg1::T
arg2
function BatteryStorage{T}(arg1,arg2) where {T}
return new{T}(arg1,arg2)
end
end
to_struct=ToStruct.tostruct(struct_A,jsonDict)
why I'm always getting :
LoadError: MethodError: no method matching struct_A(::Dict{String, Any})
caused by: MethodError: Cannot convert an object of type
Dict{String, Any} to an object of type
struct_A
Closest candidates are:
convert(::Type{T}, ::T) where T at essentials.jl:205

Parsing JSON as Array(String) in Kemal

I want to create an endpoint that receives JSON data and should parse it as an array of strings.
POST /
{
"keys": ["foo", "bar"]
}
I'm running into problems with the type system. This is what I tried (.as(Array(String))) but it does not compile:
require "kemal"
def print_keys(keys : Array(String))
puts "Got keys: #{keys}"
end
post "/" do |env|
keys = env.params.json["keys"].as(Array(String)) # <-- ERROR
print_keys(keys)
end
Kemal.run
The error message is:
8 | keys = env.params.json["keys"].as(Array(String)) # <-- ERROR
^
Error: can't cast (Array(JSON::Any) | Bool | Float64 | Hash(String, JSON::Any) | Int64 | String | Nil) to Array(String)
If I change the code to parse not Array(String) but instead String, it compiles without problems. Why does it make a difference in the .as method that the type is Array(String) instead of String?
How can the code be changed to parse arrays of strings?
I found an example in the documentation, which uses JSON.mapping. In my concrete example, it could be written as follows:
require "kemal"
def print_keys(keys : Array(String))
puts "Got keys: #{keys}"
end
class KeyMappings
JSON.mapping({
keys: Array(String)
})
end
post "/" do |env|
json = KeyMappings.from_json env.request.body.not_nil!
print_keys(json.keys)
end
Kemal.run

How to serialize Scala Map<String, MyObj> in Play Framework

So I basically have code like:
case class MyObj( value:String, unit: String)
case class ObjSeries( id: String, myObjs: Map[String, MyObj] )
However, creating writers like:
implicit val myObjWrites = Json.writes[MyObj]
implicit val objSeriesWrites: Writes[ObjSeries] = (
(JsPath \ "id").write[String] and
(JsPath \ "myObjs").write[Map[String, MyObj]]
)(unlift(ObjSeries.unapply))
Fails with " overloaded method value apply with alternatives - cannot be applied to...".
What am I missing?
The above is of course a simple example; the actual data structures I'm working on are a bit more complex. The actual example is a class:
case class ObservationSeries(
sourceId: Option[String],
geometry: Option[Point],
levels: Option[Seq[Level]],
referenceTime: Option[String],
observations: Option[Map[String, Observation]]
)
And the error message goes:
overloaded method value apply with alternatives:
[error] [B](f: B => (Option[String], Option[no.met.geometry.Point], Option[Seq[no.met.geometry.Level]], Option[String], Option[scala.collection.immutable.Map[String,models.Observation]]))(implicit fu: play.api.libs.functional.ContravariantFunctor[play.api.libs.json.OWrites])play.api.libs.json.OWrites[B] <and>
[error] [B](f: (Option[String], Option[no.met.geometry.Point], Option[Seq[no.met.geometry.Level]], Option[String], Option[scala.collection.immutable.Map[String,models.Observation]]) => B)(implicit fu: play.api.libs.functional.Functor[play.api.libs.json.OWrites])play.api.libs.json.OWrites[B]
[error] cannot be applied to (models.ObservationSeries => (Option[String], Option[no.met.geometry.Point], Option[Seq[no.met.geometry.Level]], Option[String], Option[scala.collection.Map[String,models.Observation]]))
[error] (JsPath \ "referenceTime").writeNullable[String] and
Not sure if it was a copy error, but the first class is missing the name of the second field:
# ↓↓↓
case class ObjSeries(id: String, Map[String, MyObj] )
Apart from that, what you have should work. Are you sure is this code that is failing?