Giter Site home page Giter Site logo

Comments (13)

Mohammadcore avatar Mohammadcore commented on May 20, 2024 1

@larsga I haven't got the time to test it yet. But from the quick look I had at the code changeset, looked fulfilling the requirement. Thank you very much.

from jslt.

larsga avatar larsga commented on May 20, 2024

Yes, this is expected. When you produce values with queries it's quite common to end up with null, empty arrays, empty objects, and so on. In these case JSLT simply leaves out the keys that have empty values rather than produce JSON objects that are full of junk.

from jslt.

vectro avatar vectro commented on May 20, 2024

This rationale makes sense to me, although i can think of use cases where I would want to produce a document like that. For example, I may want to produce a document like {"fooList": []} so that I don't have to special-case in the reader the situation where there are no items.

At the least this behavior should be prominently documented, IMHO.

from jslt.

larsga avatar larsga commented on May 20, 2024

Yeah, I agree that documentation is the solution for now.

I was thinking of adding an option to let people specify a filter to say what keys they want included, so that if you want to keep all keys no matter what the value is you can include a filter that says true. For example.

from jslt.

vectro avatar vectro commented on May 20, 2024

I think your example got cut off?

How about a function (or keyword) preserve() which is just like the identity function except that its result is never cleaned up? I can imagine how this could be hard to implement though, as it would require tagging the json objects somehow during construction.

from jslt.

larsga avatar larsga commented on May 20, 2024

The example is before the "For example." :)

Not sure how the preserve() would work. You mean something like:

{
  "foo" : preserve(.doesntexist),
  "bar" : .doesntexisteither
}

resulting in

{
  "foo" : null
}

?

Does that really make sense? Why is it important to keep the null in one case, but not in the other? Does it really make sense to require the null at all? So far my preference has been to just put this off until I get a better understanding of what kinds of usecases people actually have.

from jslt.

vectro avatar vectro commented on May 20, 2024

I'm not sure that I understand your proposal. Perhaps you could provide a full example? You have correctly understood mine. I'm not going to defend why somebody might want foo but not bar; I just think it's desirable to be able to produce any wanted output object.

As far as the specific use cases that might require this, the one that's in front of me is the desire to keep the types of things the same even for edge cases. So that I can guarantee a particular JSON path will always lead to an element of a certain type (be it an object, arary, etc.). This simplifies the code path on the reader side because you can just iterate whatever container you expect to be there without having to special-case null etc. My use case is not that compelling because I have control over the reader codebase so I can add that complexity there. But what if I didn't?

from jslt.

larsga avatar larsga commented on May 20, 2024

When we build objects from object expressions we filter the key/value pairs to decide which ones to leave in, and which ones to leave out. Here's the line that does the filtering: https://github.com/schibsted/jslt/blob/master/src/main/java/com/schibsted/spt/data/jslt/impl/ObjectExpression.java#L60

That NodeUtils.isValue() method is a predicate that matches what I think makes sense to include. But other people might have other ideas.

So I thought: why not let them plug in their own filter? Basically a JSLT expression that, when it evaluates to true keeps the key/value, and when it's false drops it.

The current filter can be replicated by:

. != null and not(is-object(.) and size(.) =0) and not(is-array(.) and size(.) =0)

With my proposal someone could say

. != null

That is, leave out only nulls, and keep everything else. Or they could add in ... and != "" and ditch empty strings, too.

I just think it's desirable to be able to produce any wanted output object.

I think I mostly agree with that, but the question becomes what is really wanted? And I'm not sure I know the answer yet.

As far as the specific use cases that might require this, the one that's in front of me is the desire to keep the types of things the same even for edge cases. So that I can guarantee a particular JSON path will always lead to an element of a certain type (be it an object, arary, etc.).

As in, you want to ensure that there's always {"foo" : [ ... ]} regardless of whether the ... is nothing, or some sequences of values? To my mind {"foo" : []} and {} are semantically the same thing. But I agree people might have use-cases where that's not quite true.

from jslt.

vectro avatar vectro commented on May 20, 2024

That NodeUtils.isValue() method is a predicate that matches what I think makes sense to include. But other people might have other ideas.

Makes sense to me. Only downside that I see to this approach is that the predicate may not be invoked with all the necessary context, if the rule is complex.

To my mind {"foo" : []} and {} are semantically the same thing.

Of course we had a koan about this, but I agree, they're semantically the same. Still, they are lexically different, a distinction which might arise sometimes.

from jslt.

larsga avatar larsga commented on May 20, 2024

Only downside that I see to this approach is that the predicate may not be invoked with all the necessary context, if the rule is complex.

I guess it's a bit of an open question if a rule that complex makes sense.

Your point about the code safety convenience (from the koan) is probably something that people do sometimes, but whether they should is less certain.

But, yes, where there is a distinction people might care about it.

For now I'm still inclined to wait for a concrete use-case.

from jslt.

Mohammadcore avatar Mohammadcore commented on May 20, 2024
{
  "foo" : preserve(.doesntexist),
  "bar" : .doesntexisteither
}

resulting in

{
  "foo" : null
}

This would be something we highly are in need of for a project we are working on.
is there any plans to add this feature to the library?

from jslt.

larsga avatar larsga commented on May 20, 2024

@Mohammadcore See the response to the ticket you just opened: #51

from jslt.

larsga avatar larsga commented on May 20, 2024

I consider this solved, now that we have configurable filters for which values to leave out. Please reopen if you disagree.

from jslt.

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.