Giter VIP home page Giter VIP logo

Comments (2)

MrPowerGamerBR avatar MrPowerGamerBR commented on August 12, 2024

I guess that this isn't possible to fix, this happens due to type loss, so the parser needs to do heuristics to figure out if it is a map or a array.

The problem happens when you try to use 0 on a map, because it matches the heuristics, it thinks it is a numerically indexed array... causing issues.

from jackson-dataformat-hocon.

MrPowerGamerBR avatar MrPowerGamerBR commented on August 12, 2024

It looks like @jclawson is inactive, so I guess this bug won't ever be fixed.

In the meantime, I made a custom deserializer with a hacky fix for this issue.

class FixedMapDeserializer : JsonDeserializer<Map<*, *>>() {
    override fun deserialize(p0: JsonParser, p1: DeserializationContext): Map<*, *> {
        val any = p0.readValueAsTree<TreeNode>()
        if (any.isArray) {
            val values = mutableMapOf<Any?, Any?>()
            repeat(any.size()) {
                values[it] = any.get(it)
             }
            return values
         } else {
             val values = mutableMapOf<Any?, Any?>()
             any.fieldNames().forEach {
                 values[it] = any.get(it)
             }
             return values
        }
    }
}

Then, on your HOCON Mapper

val module = SimpleModule()

module.setDeserializerModifier(object: BeanDeserializerModifier() {
      override fun modifyMapDeserializer(config: DeserializationConfig, type: MapType, beanDesc: BeanDescription, deserializer: JsonDeserializer<*>): JsonDeserializer<*> {
          return FixedMapDeserializer()
      }
})

mapper.registerModule(module)

The deserializer is a very simple & quick fix: If the JSON input is a array but Jackson expects a class, it converts the JSON to a Map (first array entry = 0 to mapValue on the map), if else, it just parses as a map.

I didn't test it on every possible combination, but on my quick tests: It works pretty well!

from jackson-dataformat-hocon.

Related Issues (8)

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.