catb0t / multifactor

track and control Factor images+executables across branches and versions
https://github.com/factor/factor
GNU General Public License v3.0
1 stars 0 forks source link

xchg::Explicit tagging doesn't make "0.99" roundtrip when decimal precision is 1 #9

Open catb0t opened 5 years ago

catb0t commented 5 years ago
$ sidef -P1 tests/06_xchg_tagging.sf 
{
   "l" : {
      "__XT" : "!B!Array.Array",
      "__XS" : "!B!String.String",
      "__XV" : [
         {
            "__XV" : "0.99"
         },
         {
            "__XV" : "1.0123123123123123123123"
         }
      ]
   },
   ...
}

Hash(
    "l" => Hash(
        "__XS" => "!B!String.String",
        "__XT" => "!B!Array.Array",
        "__XV" => [Hash("__XV" => 99/100), Hash("__XV" => 10123123123123123123123/10000000000000000000000)]
    ),
    ...
)
assert_eq(Hash(
    "l" => ["1e0", "1e0"],
    ...
), Hash(
    "l" => ["0.99", "1.0123123123123123123123"],
    ...
)) failed at tests/06_xchg_tagging.sf line 94
catb0t commented 5 years ago

there are two options to fix this with JSON::{XS,PP}'s allow_tags and FREEZE/THAW (which are the only solution short of a new JSON parser in Sidef)

  1. Alias FREEZE to ABCs::Serializable.serialize and THAW to ABCs::Serializable.unserialize, in the definition of Serializable, much simpler

  2. Add another layer of wrapping to Explicit::tag_hash / untag_hash, which uses a special class TagObject { has val; has type; has subtype; -> FREEZE {} -> THAW {} }, so that all inputs to tag_hash / untag_hash are first genericised to TagObject, which is then given to JSON::XS.allow_tags, a solution which keeps all JSON-related implementaion details out of ABCs::Serializable

There is quite a large problem still, which is that relying on allow_tags means the database JSON will not be standard syntax (("classname")[objects...])