json icon indicating copy to clipboard operation
json copied to clipboard

Runtime exception when using Decode.dict on a JSON created with Object.create(null)

Open jfmengels opened this issue 4 years ago • 0 comments

SSCCE

result = Json.Decode.decodeValue (Json.Decode.dict Json.Decode.string) jsValue

when jsValue has been created in JavaScript using Object.create(null) and sent through flags or a port.

Attempting the decoding above results in a runtime exception with this error:

Uncaught TypeError: value.hasOwnProperty is not a function
    at _Json_runHelp (VM37 workspace:1461)
    at _Json_runHelp (VM37 workspace:1478)
    at Function.f (VM37 workspace:1400)
    at A2 (VM37 workspace:56)
    at $author$project$Main$init (VM37 workspace:10540)
    at VM37 workspace:8540
    at VM37 workspace:20
    at _Platform_initialize (VM37 workspace:1877)
    at VM37 workspace:3973
    at Object.init (VM37 workspace:20)

I created an Ellie here that you can interact with: https://ellie-app.com/8ZWrTfBzWRZa1

Additional Details

  • Elm: 0.19.1
  • elm/json: 1.1.3
  • Browser: Brave

Object.create(null) is sometimes used to create a JavaScript dictionary . The difference with {} is that {} contains fields from the Object prototype, where Object.create(null) has no prototype and no fields at all.

> ({}).toString
[Function: toString]
> Object.create(null).toString
undefined

This distinction, at least in JavaScript, can be important when you use this value as a set and only care about whether the field exists.

This distinction is irrelevant when decoding, but this runtime error makes it impossible to send values that in JS-land needed to be created this way without somehow cloning them beforehand.

jfmengels avatar May 31 '20 09:05 jfmengels