IBM / JSONata4Java

Open Source Java version of JSONata
Apache License 2.0
93 stars 38 forks source link

Contextualized construction does not work #136

Open dschulten opened 3 years ago

dschulten commented 3 years ago

When I contextualize object construction, nothing will be constructed.

By "contextualize construction" I mean instead of:

{ 
    "utter": {
        "say": nested.greeting,
        "think": nested.thinking
    },
    "nestedBinding":  {
        "say": $nested.greeting,
        "think": $nested.thinking
    }
}

I want to remove duplication by pulling nested and $nested in front of the object constructor.

{ 
    "utter": nested {
        "say": greeting,
        "think": thinking
    },
    "nestedBinding": $nested {
        "say": greeting,
        "think": thinking
    }
}

This should also allow me to filter for a certain item and then work inside its context, see https://try.jsonata.org/PCo2yBJJg. That way I want to factor out filter criteria for numerous entries.

@Test
    public void supportsContextualizedConstruction() throws Exception {
        String json = "{ \n" +
            "  \"nested\": {\n" +
            "    \"greeting\": \"hello\",\n" +
            "    \"thinking\": \"bored\"\n" +
            "  } \n" +
            "}";
        JsonNode rootContextNode = JACKSON.readTree(json);

        String jsonata = "{ \n" +
            "    \"utter\": nested {\n" +
            "        \"say\": greeting,\n" +
            "        \"think\": thinking\n" +
            "    },\n" +
            "    \"nestedBinding\": $nested {\n" +
            "        \"say\": greeting,\n" +
            "        \"think\": thinking\n" +
            "    }\n"
            + "}";
        Expression jsonataExpr = Expression.jsonata(jsonata);

        ObjectNode bindingNode = JACKSON.createObjectNode();
        bindingNode //
            .put("nested",
                "{\n"
                    + "    \"nested\": {\n"
                    + "         \"greeting\": \"welcome\",\n"
                    + "         \"thinking\": \"ayaah!\" \n"
                    + "    }\n"
                    + "}");

        JsonNode transformed = jsonataExpr.evaluate(rootContextNode, bindingNode);
        System.out.println(transformed);
    }

Result: {}

Expected according to https://try.jsonata.org/Vb34AQ8j4:

{
  "utter": {
    "say": "hello",
    "think": "bored"
  },
  "nestedBinding": {
    "say": "welcome",
    "think": "ayaah!"
  }
}
wnm3 commented 3 years ago

I have your test case working. I needed to add a test for the referenced variable binding and to get the object associated with the variable name for this to work.

I've added a test named TestBindingReference to the set of tests used when building the jar files so we'll continue to test your example against future updated.

I'm releasing 1.5.2 (#145) today so in a few hours you should be able to pull this version from Maven Central at https://mvnrepository.com/artifact/com.ibm.jsonata4java/JSONata4Java

Please close this issue if you believe it has been fixed.