Code Monkey home page Code Monkey logo

Comments (9)

cowtowncoder avatar cowtowncoder commented on June 7, 2024 1

With json backend, you could enable JsonParser.Feature.STRICT_DUPLICATE_DETECTION, but I am not sure it is supported by YAML module yet. Conversely, yaml module uses SnakeYaml for decoding so it might already be checking for that.

Another relevant feature that is supported (since it's by jackson-databind, not format modules) is DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, enabling of which would throw an exception if ObjectNode (subtype of JsonNode) had duplicate keys.

from jackson-dataformats-text.

larsga avatar larsga commented on June 7, 2024

FWIW Jackson 2.8.6 supports the JsonParser flag. I just tested a YAML file with a duplicate key and got this exception:

Caused by: com.fasterxml.jackson.core.JsonParseException: Duplicate field 'Everything'
 at [Source: java.io.BufferedInputStream@621319fd; line: 7, column: 11]
	at com.fasterxml.jackson.core.json.JsonReadContext._checkDup(JsonReadContext.java:204)
	at com.fasterxml.jackson.core.json.JsonReadContext.setCurrentName(JsonReadContext.java:198)
	at com.fasterxml.jackson.dataformat.yaml.YAMLParser.nextToken(YAMLParser.java:367)
	at com.fasterxml.jackson.core.JsonParser.nextFieldName(JsonParser.java:708)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:505)
	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:362)
	at com.fasterxml.jackson.module.scala.deser.UnsortedMapDeserializer.deserialize(UnsortedMapDeserializerModule.scala:76)
	at com.fasterxml.jackson.module.scala.deser.UnsortedMapDeserializer.deserialize(UnsortedMapDeserializerModule.scala:39)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2915)

So I think this issue can be closed.

from jackson-dataformats-text.

cowtowncoder avatar cowtowncoder commented on June 7, 2024

@larsga Thank you for checking that -- I went ahead and added a unit test to confirm this, and guard against regression.

from jackson-dataformats-text.

nagarajasr avatar nagarajasr commented on June 7, 2024

it doesn't seem to work for me.

jackson-dataformat-yaml:2.9.2
jackson-databind:2.9.3

here is the code:

ObjectMapper objMapper = new ObjectMapper(new YAMLFactory()).enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION); 
		reader = objMapper.readerFor(ResponseParameterGroup[].class);

here is my YAML. The second 'response_parameters' seems to override the first one, while i expect an exception to occur:

- parameter_group_name: GROUP1 ## there are several groups showing just one for brevity
  response_parameters: 
  - parameter_name: ObjectKey
    output:
      output_type: value
      output_name: name
    filters:
      - filter_type: object_type_filter
        filter_params:
          object_type: shelf
      - filter_type: data_filter
        filter_params:
            object_type: shelf
            criteria_name: type
            criteria_value: CPE1
  response_parameters:  ## this is the duplicate but i dont get any errors, this one seems to override the previous one
  - parameter_name: ObjectKey
    output:
      output_type: value
      output_name: name
    filters:
      - filter_type: object_type_filter
        filter_params:
          object_type: shelf
      - filter_type: data_filter
        filter_params:
            object_type: shelf
            criteria_name: type
            criteria_value: CPE2

what am i doing wrong?

from jackson-dataformats-text.

cowtowncoder avatar cowtowncoder commented on June 7, 2024

I think I'd need to see definition of ResponseParameterGroup, and call to read value(s) (you did include ObjectReader but just need to see how it is called).

from jackson-dataformats-text.

nagarajasr avatar nagarajasr commented on June 7, 2024

hope this helps:

https://gist.github.com/nagarajasr/5c732575094601e678524d4a978cb865

DataPopulatorInputEntity is an empty marker interface.

and call to read values is along these lines:

public class DataPopulatorInputHelper<T extends DataPopulatorInputEntity> {
    public T[] getInputRecords(Reader dpifr) throws DataPopulatorException {
		T[] inputRecords = null;

		try {
			inputRecords = reader.readValue(dpifr);
		} catch (....) {
                      // handle exceptions
                }
    }
}

thanks for your help!

from jackson-dataformats-text.

cowtowncoder avatar cowtowncoder commented on June 7, 2024

Ok. since it seems there is a remaining problem could you please file a new issue, for dup detection not working?
Ideally it'd be great to have simplified test case; I don't think complexity of model should matter here.

from jackson-dataformats-text.

Hzj-jie avatar Hzj-jie commented on June 7, 2024

Just in case someone reaches this page from google. Same case here, YAMLFactory -> ObjectMapper -> JsonNode, DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY works. It's reasonable to be controlled by a deserialization feature, as you do not "parse" json, but "deserialize" objects from YAML parser.

from jackson-dataformats-text.

cowtowncoder avatar cowtowncoder commented on June 7, 2024

@Hzj-jie ah! Thank you for pointing that out, that should be helpful.

from jackson-dataformats-text.

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.