I've got this working with std/marshal, but would rather use json for the 
portability and compatibility with object structure additions in the future

I'm trying to serialize the SaveSlotEntity type. Using this code:
    
    
    type
      LevelProgressEntity* = ref object of RootObj
        levelId: string
        bestTime: Option[float]
        hasCollectedStar: bool
      
      SaveSlotEntity* = ref object of RootObj
        progress: Table[string, LevelProgressEntity]
        modelVersion: int
    
    
    proc saveJson*[T](value: T, path: string) {.raises:[].} =
      let testObj = SaveSlotEntity(progress: initTable[string, 
LevelProgressEntity](), modelVersion: 1)
      let jsonNode = %testObj
      let jsonStr = $jsonNode
      let bytes: seq[byte] = cast[seq[byte]](jsonStr)
      [..] more code to write file
    
    
    Run

Crashes with the following:
    
    
    
/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/src/data_store/configuration.nim(14,
 11) template/generic instantiation of `saveJson` from here
    
/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/src/common/json_utils.nim(23,
 18) template/generic instantiation of `%` from here
    /Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/lib/pure/json.nim(404, 
14) template/generic instantiation of `%` from here
    /Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/lib/pure/json.nim(397, 
41) template/generic instantiation of `%` from here
    /Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/lib/pure/json.nim(369, 
15) Error: type mismatch: got <Table[system.string, 
json_utils.LevelProgressEntity]>
    but expected one of:
    iterator pairs(a: cstring): tuple[key: int, val: char]
      first type mismatch at position: 1
      required type for a: cstring
      but expression 'table' is of type: Table[system.string, 
json_utils.LevelProgressEntity]
    iterator pairs(a: string): tuple[key: int, val: char]
      first type mismatch at position: 1
      required type for a: string
      but expression 'table' is of type: Table[system.string, 
json_utils.LevelProgressEntity]
    iterator pairs(node: JsonNode): tuple[key: string, val: JsonNode]
      first type mismatch at position: 1
      required type for node: JsonNode
      but expression 'table' is of type: Table[system.string, 
json_utils.LevelProgressEntity]
    iterator pairs[IX, T](a: array[IX, T]): tuple[key: IX, val: T]
      first type mismatch at position: 1
      required type for a: array[IX, T]
      but expression 'table' is of type: Table[system.string, 
json_utils.LevelProgressEntity]
    iterator pairs[T](a: openArray[T]): tuple[key: int, val: T]
      first type mismatch at position: 1
      required type for a: openArray[T]
      but expression 'table' is of type: Table[system.string, 
json_utils.LevelProgressEntity]
    iterator pairs[T](a: seq[T]): tuple[key: int, val: T]
      first type mismatch at position: 1
      required type for a: seq[T]
      but expression 'table' is of type: Table[system.string, 
json_utils.LevelProgressEntity]
    iterator pairs[T](this: SDKArray[T]): tuple[key: int, val: T]
      first type mismatch at position: 1
      required type for this: SDKArray[pairs.T]
      but expression 'table' is of type: Table[system.string, 
json_utils.LevelProgressEntity]
    
    expression: pairs(table)
         Error: Build failed for package: wheelsprung
            ... Execution failed with exit code 256
            ... Command: /Users/ninovanhooff/.nimble/bin/nim c --colors:on 
--noNimblePath -d:simulator -d:debug -d:NimblePkgVersion=0.2.0 
--path:'/Users/ninovanhooff/.nimble/pkgs/playdate-#main' 
--path:/Users/ninovanhooff/.nimble/pkgs/chipmunk7-7.0.3 
-o:/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/wheelsprung
 
/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/level-data/src/wheelsprung.nim
    stack trace: (most recent call last)
    
/private/var/folders/ww/sx_qg7z51jz6bgr15k4_5y8m0000gn/T/nimblecache-3905360933/nimscriptapi_3772293278.nim(187,
 16)
    
/Users/ninovanhooff/PlaydateProjects/playdate-nim/src/playdate/build/nimble.nim(74,
 16) simulatorTask
    
/Users/ninovanhooff/PlaydateProjects/playdate-nim/src/playdate/build/utils.nim(22,
 10) nimble
    
/Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/lib/system/nimscript.nim(273,
 7) exec
    
/Users/ninovanhooff/.choosenim/toolchains/nim-1.6.16/lib/system/nimscript.nim(273,
 7) Error: unhandled exception: FAILED: nimble -d:simulator -d:debug build 
--verbose [OSError]
         Error: Exception raised during nimble script execution
    
    
    Run

Should I write some custom hooks that tell json how to transform 
LevelPropgressEntity to JSONNode? I could use some help with that

Reply via email to