Hello All, This could be related to issue #6384 <https://github.com/golang/go/issues/6384>
I want to JSON encode a float64 point number always as a JSON float, so 1.0 should be encoded as 1.0 not as 1 because at decoding time I want to distinguish them. Conceptually, I am persisting in JSON format something which is somehow close to some AST, or even to S-expressions (see below for details). So I want a float 1.0 to be different of the integer 1. If you are curious, my exact code is commit 096ec00f53011b2a1b05ce <https://github.com/bstarynk/monimelt/commit/096ec00f53011b2a1b05ceb8d14d4e65e88285d5> on github, and when running go test -v objvalmo with my monimelt project being last in my GOPATH I'm getting as output (amongst many other lines) json_emit v=-1.000000 of type objvalmo.FloatV json_emit buf: -1 and I would like that second line to be at least json_emit buf: -1.0 because I need to separate objvalmo.FloatV from objvalmo.IntV types I played with the following json/encoding example <https://golang.org/pkg/encoding/json/#example_RawMessage_marshal> and changed it (by adding some mass being 1.0) to package main import ( "encoding/json" "fmt" "os" ) func main() { h := json.RawMessage(`{"precomputed": true}`) c := struct { Header *json.RawMessage `json:"header"` Body string `json:"body"` Mass float64 `json:"mass"` }{Header: &h, Body: "Hello Gophers!", Mass: 1.0} b, err := json.MarshalIndent(&c, "", "\t") if err != nil { fmt.Println("error:", err) } os.Stdout.Write(b) } and expected the output to contain "mass" : 1.0 but it still gives a line with only "mass": 1 without any decimal point. Is there any way to change that and get that decimal point? More generally, I don't understand well how should I use JSON facilities in Go. IMHO, there are two approaches: one is using go reflection facilities, and claim to be able to serialize most arbitrary values. another is not using them at all (and this is what I would prefer) by constructing some explicit data in memory mimicking JSON format. FWIW, I care a bit about performance. To explain my issues, let us imagine that I want to serialize (and unserialize) in JSON format some s-expressions <https://en.wikipedia.org/wiki/S-expression> (or some program of a micro Scheme or micro Lisp interpreter), with both lists and vectors as composite types, with the additional constraint that some symbols should not be serialized (and we'll output the null JSON value for them instead) For simplicity assume that we have only alphabetical symbols like xy foo bar efg Let us suppose that we want to ignore all symbols starting with a vowel (so we ignore efg here). so (foo 1 #(2 xy) (bar "ab" efg -1.0 "cd")) is an example of such s-expr.. It is a list of 4 elements. The third element is a vector #(2 xy) of 2 components. The 4th and last element is a list (bar "ab" efg -1.0 "cd") of 5 elements whose 3rd element is the symbol efg which, since starting with a vowel, should be ignored and serialized as null and whose 4th element is the floating point -1.0 (not the integer -1). I would like to serialize that S-expr value as e.g. { "list" : [ { "symb" : "foo" }, 1, { "vect" : [ 2, { "symb" : "xy" } ] }, { "list" : [ { "symb" : "bar" }, null, -1.0, "cd" ] } ] } Actually, I am not exacty coding a microScheme. I haved shared objects (of *ObjectMo type) and values (of ValueMo type) inside objects (every object has a map and a slice of values). I want to persist them in JSON inside an Sqlite database, but some objects are transient and non-persistent (so I would encode them as null). All objects have a *unique* id like _0ECuE7b9XhQ_3HvWl3RGhD6 Any advices are welcome. In particular, I am a bit afraid of the reflection approach (because I want serialization & deserialization to not be too slow, and I really need to master the JSON representation). -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
