Git Product home page Git Product logo

Comments (12)

jrudolph avatar jrudolph commented on September 6, 2024

Can you post a test case / test data to show which behaviour you want exactly?

from json-lenses.

analytically avatar analytically commented on September 6, 2024

https://gist.github.com/analytically/0b3ac6978f209c91d6b8

from json-lenses.

analytically avatar analytically commented on September 6, 2024

The usercase:

https://github.com/coenrecruitment/capsulecrm-ciscoipphonedir/blob/master/src/main/scala/uk/co/coen/ciscoipphonedir/CapsuleCRMCiscoIPPhoneDirectoryService.scala

Unfortunately Capsule CRM returns single element arrays without wrapping it in a JSON array []. So it currently fails when it returns a single organisation and/or a single person.

from json-lenses.

analytically avatar analytically commented on September 6, 2024

New link: https://github.com/coenrecruitment/capsulecrm-ciscoipphonedir/blob/master/src/main/scala/uk/co/coen/ciscoipphonedir/Main.scala

from json-lenses.

jrudolph avatar jrudolph commented on September 6, 2024

I added a new lens arrayOrSingletonAsArray which won't touch an array but will lift each other json value into a singleton array. Could you try the new 0.5.4-SNAPSHOT version which has the change?

resolvers += "sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots"

libraryDependencies += "net.virtual-void" %% "json-lenses" % "0.5.4-SNAPSHOT"

from json-lenses.

analytically avatar analytically commented on September 6, 2024

Working, great! Thanks! analytically/capsulecrm-ciscoipphonedir@99f0fd5

from json-lenses.

analytically avatar analytically commented on September 6, 2024

In my opinion this should better be a config switch to treat all single elements as arrays. Perhaps something you can configure on the JsonParser? Now I'm inserting arrayOrSingletonAsArray everywhere, which looks half baked.

eg. 'parties / optionalField("organisation") / arrayOrSingletonAsArray / filter(('contacts / 'phone).is[JsValue](_ => true)) / 'id

from json-lenses.

analytically avatar analytically commented on September 6, 2024

Better example: { json.extract[String]('parties / 'organisation / arrayOrSingletonAsArray / filter('id.is[String](_ == id)) / 'contacts / 'phone / arrayOrSingletonAsArray / element(0) / 'phoneNumber) }

from json-lenses.

jrudolph avatar jrudolph commented on September 6, 2024

Yeah, I've seen your code and there's definitely still lots of stuff you could improve in json-lenses to make it more succinct. That said, the main improvement in your code would come from reusing previously build lenses. That's the big advantage of lenses: they are completely composable and first-class values. You can put

'parties / 'organisation / arrayOrSingletonAsArray

into a variable and reuse it whereever you need it.

In my opinion this should better be a config switch to treat all single elements as arrays.

I don't think that would work because a rule like this would have to be applied everywhere. What you are requesting is that every field value has to be an array. In fact, that would make your use-case much more complicated than it is now because in the normal case you don't expect field values to be an array.

from json-lenses.

analytically avatar analytically commented on September 6, 2024

Understood. But I wasn't clear; not every field value, but every JSON child object. This issue stems from XML-to-JSON converters that wrap single XML child entities as single JSON child objects, hence not wrapping it in a array. Therefore one can assume this to be the case everywhere for that parse.

from json-lenses.

jrudolph avatar jrudolph commented on September 6, 2024

Even then, it would mean your lens would have to look like this:

'parties / element(0) / 'organisation / filter('id.is[String](_ == id)) / 'contacts / element(0) / 'phone / element(0) / 'phoneNumber)

I don't see this as a major improvement, it just makes something implicit what really is an explicit requirement of your data-model and should be treated explicitly IMO.

from json-lenses.

analytically avatar analytically commented on September 6, 2024

Ok agreed. I've simplified the code and it looks better indeed.

from json-lenses.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.