Check json in scala with lift-json

I would like to verify that json contains a list of keys / values ​​before trying to marshal the case class using lift-json. Data may be nested.

For instance:

{ "name": "stack", "desc": "desc", "age": 29, "address": { "street": "123 elm", "postal_code": 22222, "city": "slc", } } 

How can I verify that this JSON contains values ​​for "name", "age" and "address \ street"? Suppose all other fields are optional.

Sorry if I'm missing something obvious, but I suspect something like this has been resolved before.

By the way, will anyone try to Order? https://github.com/nparry/orderly4jvm

+4
source share
5 answers

As I can see, you have several options:

  • You can use the low-level parser API parser lift-json (find this phrase on this page ) to capture every field you care about. If you have not received all the fields you want, at the moment when you encounter an End token, you throw an exception. If you have the required field, then create your object.

    Pro : it only parses JSON once. This is the most efficient way to use lift-json.

    Con You have created fragile manual software.

  • You can use lift-json JsonAST, which is the normal result of calling its parse method (searching for “Parsing JSON” on this page ) and then expressing it like XPath (searching for “XPath + HOFs” on this page ) to determine if whether fields are present. If they are, create your object by passing the appropriate fields to the constructor.

    Pro : less manual rental than # 1. Analyzes data only once.

    Con : it's not as fast as # 2.

  • Use the methods from # 1 or # 2 to determine if the required fields are available. If so, then use the liftinson deserialization (look for the “Serialization” heading on this page ) to create both the object.

    Pro : I really stretch here ... If you expected that most of the data would be bad, you can determine that before entering the slightly more difficult lifting process, json deserialization

    Con You double-analyzed the data if the data is valid.

  • Just use the liftinson deserialization (find the “Serialization” heading on this page ). This is more or less what is described in No. 2, except that it uses reflection to find out which fields are needed.

    Pro : this is the most suitable solution

    Con : he's slower than # 1 and # 2.

My recommendation: if you absolutely don't need maximum performance, use # 4. If you really need speed, use # 1. Another advantage of using a style solution # 1 or # 2 is that they allow you to perform odd data enforcement, for example mapping two alternative field names in JSON to one field in a scala object.

+4
source

This worked for me: JSON for XML in Scala and working with the result of Option ()

Here is a very simple clipping and pasting from the previous question. This does not meet your requirements, but may give you some ideas:

 import util.parsing.json.JSON._ object JsonSoap { def main(args: Array[String]) { val x = parseFull("""{"name":"Joe","surname":"SOAP"}""") val y = x collect { case m: Map[_, _] => m collect { case (key: String, value: String) => key -> value } } val z = for (m <- y; name <- m.get("name"); surname <- m.get("surname")) yield { <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Person> <Name>{name}</Name> <Surname>{surname}</Surname> </Person> </soap:Body> </soap:Envelope> } println(z) } } 

You looked at them:

Parse JSON in Scala using standard Scala

JSON parsing and iteration using an object

Simple JSON for XML

0
source

You basically check the circuit, so create a handle that matches your object and go through the object using JsonAST, confirming that there are correct elements at each level. How you structure your validator is up to you, but I would mirror the types so that you can go with both asth and the validator at the same time.

0
source

I developed this json parser / validator https://github.com/HigherState/jameson

0
source

Our team began to look at the orders. For the JSON objects we use, OrderLy can sufficiently describe them. I think we will use them.

In the Orderly library you mentioned: https://github.com/nparry/orderly4jvm seems very good. It gives decent error messages when validation fails.

We covered the use of Json Schema, especially the json-schema-validator Java library: https://github.com/fge/json-schema-validator

The error messages were not descriptive.

0
source

Source: https://habr.com/ru/post/1398723/


All Articles