From 57b4518564815b93c2f069dd96ab4fb5240cb1a4 Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Sun, 20 Mar 2016 19:55:23 +0100 Subject: [PATCH] Fixed presenter bug * Fixed TagIds for Nim's system types * Output JSON integers for Nim's integer types * Output JSON floats for Nim's float types --- private/presenter.nim | 12 ++++++---- private/serialization.nim | 48 --------------------------------------- private/tagLibrary.nim | 12 ++++++++++ yaml.nim | 39 +++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 52 deletions(-) diff --git a/private/presenter.nim b/private/presenter.nim index 5509d8b..fa803bf 100644 --- a/private/presenter.nim +++ b/private/presenter.nim @@ -366,7 +366,7 @@ proc present*(s: var YamlStream, target: Stream, tagLib: TagLibrary, if options.style == psJson: let hint = guessType(item.scalarContent) if item.scalarTag in [yTagQuestionMark, yTagBoolean] and - hint in [yTypeBoolTrue, yTypeBoolFalse]: + hint in {yTypeBoolTrue, yTypeBoolFalse}: if hint == yTypeBoolTrue: safeWrite("true") else: @@ -374,11 +374,15 @@ proc present*(s: var YamlStream, target: Stream, tagLib: TagLibrary, elif item.scalarTag in [yTagQuestionMark, yTagNull] and hint == yTypeNull: safeWrite("null") - elif item.scalarTag in [yTagQuestionMark, yTagInteger] and + elif item.scalarTag in [yTagQuestionMark, yTagInteger, + yTagNimInt8, yTagNimInt16, yTagNimInt32, yTagNimInt64, + yTagNimUInt8, yTagNimUInt16, yTagNimUInt32, + yTagNimUInt64] and hint == yTypeInteger: safeWrite(item.scalarContent) - elif item.scalarTag in [yTagQuestionMark, yTagFloat] and - hint in [yTypeFloatInf, yTypeFloatNaN]: + elif item.scalarTag in [yTagQuestionMark, yTagFloat, + yTagNimFloat32, yTagNimFloat64] and + hint in {yTypeFloatInf, yTypeFloatNaN}: raise newException(YamlPresenterJsonError, "Infinity and not-a-number values cannot be presented as JSON!") elif item.scalarTag in [yTagQuestionMark, yTagFloat] and diff --git a/private/serialization.nim b/private/serialization.nim index a3c3189..d9347e9 100644 --- a/private/serialization.nim +++ b/private/serialization.nim @@ -18,59 +18,11 @@ proc newSerializationContext(s: AnchorStyle): SerializationContext = result.refs = initTable[pointer, AnchorId]() result.style = s result.nextAnchorId = 0.AnchorId - -proc initSerializationTagLibrary(): TagLibrary {.raises: [].} = - result = initTagLibrary() - result.tags["!"] = yTagExclamationMark - result.tags["?"] = yTagQuestionMark - result.tags["tag:yaml.org,2002:str"] = yTagString - result.tags["tag:yaml.org,2002:null"] = yTagNull - result.tags["tag:yaml.org,2002:bool"] = yTagBoolean - result.tags["tag:yaml.org,2002:float"] = yTagFloat - result.tags["tag:yaml.org,2002:timestamp"] = yTagTimestamp - result.tags["tag:yaml.org,2002:value"] = yTagValue - result.tags["tag:yaml.org,2002:binary"] = yTagBinary - -var - serializationTagLibrary* = initSerializationTagLibrary() ## \ - ## contains all local tags that are used for type serialization. Does - ## not contain any of the specific default tags for sequences or maps, - ## as those are not suited for Nim's static type system. - ## - ## Should not be modified manually. Will be extended by - ## `serializable <#serializable,stmt,stmt>`_. template presentTag*(t: typedesc, ts: TagStyle): TagId = ## Get the TagId that represents the given type in the given style if ts == tsNone: yTagQuestionMark else: yamlTag(t) -template setTagUriForType*(t: typedesc, uri: string): stmt = - ## Associate the given uri with a certain type. This uri is used as YAML tag - ## when loading and dumping values of this type. - let id {.gensym.} = serializationTagLibrary.registerUri(uri) - proc yamlTag*(T: typedesc[t]): TagId {.inline, raises: [].} = id - ## autogenerated - -template setTagUriForType*(t: typedesc, uri: string, idName: expr): stmt = - ## Like `setTagUriForType <#setTagUriForType,typedesc,string>`_, but lets - ## you choose a symbol for the `TagId <#TagId>`_ of the uri. This is only - ## necessary if you want to implement serialization / construction yourself. - let idName* = serializationTagLibrary.registerUri(uri) - proc yamlTag*(T: typedesc[t]): TagId {.inline, raises: [].} = - ## autogenerated - -setTagUriForType(char, "!nim:system:char", yTagNimChar) -setTagUriForType(int8, "!nim:system:int8", yTagNimInt8) -setTagUriForType(int16, "!nim:system:int16", yTagNimInt16) -setTagUriForType(int32, "!nim:system:int32", yTagNimInt32) -setTagUriForType(int64, "!nim:system:int64", yTagNimInt64) -setTagUriForType(uint8, "!nim:system:uint8", yTagNimUInt8) -setTagUriForType(uint16, "!nim:system:uint16", yTagNimUInt16) -setTagUriForType(uint32, "!nim:system:uint32", yTagNimUInt32) -setTagUriForType(uint64, "!nim:system:uint64", yTagNimUInt64) -setTagUriForType(float32, "!nim:system:float32", yTagNimFloat32) -setTagUriForType(float64, "!nim:system:float64", yTagNimFloat64) - proc lazyLoadTag(uri: string): TagId {.inline, raises: [].} = try: result = serializationTagLibrary.tags[uri] except KeyError: result = serializationTagLibrary.registerUri(uri) diff --git a/private/tagLibrary.nim b/private/tagLibrary.nim index 7862fc9..e659685 100644 --- a/private/tagLibrary.nim +++ b/private/tagLibrary.nim @@ -65,3 +65,15 @@ proc initExtendedTagLibrary(): TagLibrary = result.tags["tag:yaml.org,2002:timestamp"] = yTagTimestamp result.tags["tag:yaml.org,2002:value"] = yTagValue result.tags["tag:yaml.org,2002:yaml"] = yTagYaml + +proc initSerializationTagLibrary(): TagLibrary = + result = initTagLibrary() + result.tags["!"] = yTagExclamationMark + result.tags["?"] = yTagQuestionMark + result.tags["tag:yaml.org,2002:str"] = yTagString + result.tags["tag:yaml.org,2002:null"] = yTagNull + result.tags["tag:yaml.org,2002:bool"] = yTagBoolean + result.tags["tag:yaml.org,2002:float"] = yTagFloat + result.tags["tag:yaml.org,2002:timestamp"] = yTagTimestamp + result.tags["tag:yaml.org,2002:value"] = yTagValue + result.tags["tag:yaml.org,2002:binary"] = yTagBinary \ No newline at end of file diff --git a/yaml.nim b/yaml.nim index 72b8aa6..f776075 100644 --- a/yaml.nim +++ b/yaml.nim @@ -496,6 +496,8 @@ proc initExtendedTagLibrary*(): TagLibrary {.raises: [].} ## - ``!!value`` ## - ``!!yaml`` +proc initSerializationTagLibrary(): TagLibrary {.raises: [].} + proc guessType*(scalar: string): TypeHint {.raises: [].} ## Parse scalar string according to the RegEx table documented at ## `TypeHint <#TypeHind>`_. @@ -605,6 +607,43 @@ proc dump*[K](value: K, target: Stream, tagStyle: TagStyle = tsRootOnly, {.raises: [YamlPresenterJsonError, YamlPresenterOutputError].} ## Dump a Nim value as YAML character stream. +var + serializationTagLibrary* = initSerializationTagLibrary() ## \ + ## contains all local tags that are used for type serialization. Does + ## not contain any of the specific default tags for sequences or maps, + ## as those are not suited for Nim's static type system. + ## + ## Should not be modified manually. Will be extended by + ## `serializable <#serializable,stmt,stmt>`_. + +template setTagUriForType*(t: typedesc, uri: string): stmt = + ## Associate the given uri with a certain type. This uri is used as YAML tag + ## when loading and dumping values of this type. + let id {.gensym.} = serializationTagLibrary.registerUri(uri) + proc yamlTag*(T: typedesc[t]): TagId {.inline, raises: [].} = id + ## autogenerated + +template setTagUriForType*(t: typedesc, uri: string, idName: expr): stmt = + ## Like `setTagUriForType <#setTagUriForType,typedesc,string>`_, but lets + ## you choose a symbol for the `TagId <#TagId>`_ of the uri. This is only + ## necessary if you want to implement serialization / construction yourself. + let idName* = serializationTagLibrary.registerUri(uri) + proc yamlTag*(T: typedesc[t]): TagId {.inline, raises: [].} = idName + ## autogenerated + +# tags for Nim's standard types +setTagUriForType(char, "!nim:system:char", yTagNimChar) +setTagUriForType(int8, "!nim:system:int8", yTagNimInt8) +setTagUriForType(int16, "!nim:system:int16", yTagNimInt16) +setTagUriForType(int32, "!nim:system:int32", yTagNimInt32) +setTagUriForType(int64, "!nim:system:int64", yTagNimInt64) +setTagUriForType(uint8, "!nim:system:uint8", yTagNimUInt8) +setTagUriForType(uint16, "!nim:system:uint16", yTagNimUInt16) +setTagUriForType(uint32, "!nim:system:uint32", yTagNimUInt32) +setTagUriForType(uint64, "!nim:system:uint64", yTagNimUInt64) +setTagUriForType(float32, "!nim:system:float32", yTagNimFloat32) +setTagUriForType(float64, "!nim:system:float64", yTagNimFloat64) + # implementation include private.tagLibrary