Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json % operator can't construct JsonNode from tuples. #16851

Closed
HapeniaLans opened this issue Jan 28, 2021 · 1 comment
Closed

Json % operator can't construct JsonNode from tuples. #16851

HapeniaLans opened this issue Jan 28, 2021 · 1 comment

Comments

@HapeniaLans
Copy link

Function echo outputs the wrong string.

Example

import json
let foo = (key1: 10, key2: 5)
echo $ %foo
# This code should be a minimum reproducible example:
# try to simplify and minimize as much as possible. If it's a compiler
# issue, try to minimize further by removing any imports if possible.

Current Output

please check whether the problem still exists in git head before posting,
see rebuilding the compiler.

Error: type mismatch: got <tuple[key1: int, key2: int]>
but expected one of: 
proc `%`(b: bool): JsonNode
  first type mismatch at position: 1
  required type for b: bool
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode
  first type mismatch at position: 1
  required type for keyVals: openArray[tuple[key: string, val: JsonNode]]
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`(n: BiggestInt): JsonNode
  first type mismatch at position: 1
  required type for n: BiggestInt
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`(n: BiggestUInt): JsonNode
  first type mismatch at position: 1
  required type for n: BiggestUInt
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`(n: float): JsonNode
  first type mismatch at position: 1
  required type for n: float
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`(n: int): JsonNode
  first type mismatch at position: 1
  required type for n: int
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`(n: uint): JsonNode
  first type mismatch at position: 1
  required type for n: uint
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`(o: enum): JsonNode
  first type mismatch at position: 1
  required type for o: enum
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`(o: ref object): JsonNode
  first type mismatch at position: 1
  required type for o: ref object
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`(s: string): JsonNode
  first type mismatch at position: 1
  required type for s: string
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`[T: object](o: T): JsonNode
  first type mismatch at position: 1
  required type for o: T: object
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`[T](elements: openArray[T]): JsonNode
  first type mismatch at position: 1
  required type for elements: openArray[T]
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`[T](opt: Option[T]): JsonNode
  first type mismatch at position: 1
  required type for opt: Option[%.T]
  but expression 'foo' is of type: tuple[key1: int, key2: int]    
proc `%`[T](table: Table[string, T] | OrderedTable[string, T]): Js  required type for table: Table[system.string, %.T] or OrderedTable[system.string, %.T]
  but expression 'foo' is of type: tuple[key1: int, key2:rderedTab int]
template `%`(j: JsonNode): JsonNode                       int]    
  first type mismatch at position: 1
  required type for j: JsonNode
  but expression 'foo' is of type: tuple[key1: int, key2: int]                                                     int]    

expression: %foo

Expected Output

{"key1": 10, "key2": 5}

Possible Solution

In the file json.nim, line 370:

proc `%`*[T: object](o: T): JsonNode =
  ## Construct JsonNode from tuples and objects.
  result = newJObject()
  for k, v in o.fieldPairs: result[k] = %v

Change the generic type to [T: object or tuple] can perfectly solve this problem.

proc `%`*[T: object or tuple](o: T): JsonNode =
  ## Construct JsonNode from tuples and objects.
  result = newJObject()
  for k, v in o.fieldPairs: result[k] = %v

Additional Information

Current nim version:

$ nim -v
Nim Compiler Version 1.4.2 [Windows: amd64]
Compiled at 2020-11-30
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release
@ringabout
Copy link
Member

ringabout commented Jan 28, 2021

duplicated issue:
see #12290

You could use jsonutils as a workaround

import std/[json,jsonutils]

let foo = (key1: 10, key2: 5)
echo foo.toJson

print

{"key1":10,"key2":5}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants