From 5c23f10e50dbd620bb7848b3fa9715cbf6d4cda2 Mon Sep 17 00:00:00 2001 From: Jon Harper Date: Fri, 2 Jan 2015 19:39:14 +0100 Subject: [PATCH] json.writer: add an explicit error when serializing fp-specials --- basis/json/json.factor | 5 ++++- basis/json/writer/writer-docs.factor | 7 ++++++- basis/json/writer/writer.factor | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/basis/json/json.factor b/basis/json/json.factor index b68290ba28..b60652bcb0 100644 --- a/basis/json/json.factor +++ b/basis/json/json.factor @@ -1,10 +1,13 @@ -USING: kernel vocabs ; +USING: kernel vocabs summary debugger io ; IN: json SINGLETON: json-null ERROR: json-error ; +ERROR: json-fp-special-error value ; +M: json-fp-special-error summary drop "JSON serialization: illegal float:" ; + : if-json-null ( x if-null else -- ) [ dup json-null? ] [ [ drop ] prepose ] diff --git a/basis/json/writer/writer-docs.factor b/basis/json/writer/writer-docs.factor index e027e4d1c8..6d441cfc57 100644 --- a/basis/json/writer/writer-docs.factor +++ b/basis/json/writer/writer-docs.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2006 Chris Double. ! See http://factorcode.org/license.txt for BSD license. -USING: help.markup help.syntax kernel ; +USING: json help.markup help.syntax kernel ; IN: json.writer HELP: >json @@ -23,6 +23,11 @@ $nl } { $see-also >json } ; +{ json-fp-special-error json-allow-fp-special? } related-words + +HELP: json-fp-special-error +{ $error-description "Thrown by " { $link "json.writer" } " when attempting to serialize -1/0. or +1/0. or NaN when " { $link json-allow-fp-special? } " is not enabled." } ; + ARTICLE: "json.writer" "JSON writer" "The " { $vocab-link "json.writer" } " vocabulary defines words for converting objects to JSON format." { $subsections diff --git a/basis/json/writer/writer.factor b/basis/json/writer/writer.factor index f7bc35ec6e..9795b993e1 100644 --- a/basis/json/writer/writer.factor +++ b/basis/json/writer/writer.factor @@ -94,7 +94,7 @@ M: integer stream-json-print : float>json ( float -- string ) dup fp-special? [ - json-allow-fp-special? get [ json-error ] unless + json-allow-fp-special? get [ json-fp-special-error ] unless { { [ dup fp-nan? ] [ drop "NaN" ] } { [ dup 1/0. = ] [ drop "Infinity" ] }