migamake / json-autotype

Automatic Haskell type inference from JSON input
149 stars 23 forks source link

Do not instantly decide that Ints are Doubles #11

Closed fredmorcos closed 6 years ago

fredmorcos commented 8 years ago

I am not sure if this is within scope of the project: If I provide an input file with an array of JSON objects, where the objects have identical properties, I noticed that the integer properties are inferred as Doubles.

Would it be possible to assume these values to be integers first, and only when a double value shows up for the property that the type is promoted to a Double?

Here is a minimal example:

foo.txt

and the outcome is:

foo-toplevelelt.txt

As you can see, topLevelEltProperty1 is of type Double while property1 in the input JSON file is never given a double value (ie, all are integers).

I expect that topLevelEltProperty1 is of type Int or Integer.

mgajda commented 8 years ago

Absolutely is. I have previously started designing for subset types, but did not finish. Ideally that would allow for distinguishing those Strings that are solely Dates, and those numbers that are always Integers.

Patches welcome, unless you are patient enough to wait for my own :-)

fredmorcos commented 8 years ago

Ideally that would allow for distinguishing those Strings that are solely Dates

Not sure what you mean by that, since JSON has no notion of Date vs String... but it does have a notion of Int vs Int + Frac (Float or Double).

Anyways, I'll wait for you instead :)

mgajda commented 8 years ago

There are many JSON APIs that use subset of Strings that parse to some other type:

  1. UUID
  2. Date
  3. Timestamp
  4. URL
  5. Very short list of constants (just like C's enum type.)

All these should be recognized with the subset typing, we just need to make sure that subset have either no overlap at all, or one of them is strict subset of the other.

mgajda commented 7 years ago

Please go forward if you have time. I am currently focusing my efforts in broader context of AI and Data Science in Haskell.

mgajda commented 6 years ago

This one is fixed with release 3.0.0.