From dac7740a27a435fd647792160e9c75daf5889fe7 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 11 Mar 2009 12:50:58 -0700 Subject: document json R=r DELTA=115 (102 added, 0 deleted, 13 changed) OCL=25953 CL=26128 --- src/lib/json/struct.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src/lib/json/struct.go') diff --git a/src/lib/json/struct.go b/src/lib/json/struct.go index 82e0a80a7..cfba2ce6d 100644 --- a/src/lib/json/struct.go +++ b/src/lib/json/struct.go @@ -202,6 +202,61 @@ func (b *_StructBuilder) Key(k string) Builder { return nobuilder } +// Unmarshal parses the JSON syntax string s and fills in +// an arbitrary struct or array pointed at by val. +// It uses the reflection library to assign to fields +// and arrays embedded in val. Well-formed data that does not fit +// into the struct is discarded. +// +// For example, given the following definitions: +// +// type Email struct { +// where string; +// addr string; +// } +// +// type Result struct { +// name string; +// phone string; +// emails []Email +// } +// +// var r = Result{ "name", "phone", nil } +// +// unmarshalling the JSON syntax string +// +// { +// "email": [ +// { +// "where": "home", +// "addr": "gre@example.com" +// }, +// { +// "where": "work", +// "addr": "gre@work.com" +// } +// ], +// "name": "Grace R. Emlin", +// "address": "123 Main Street" +// } +// +// via Unmarshal(s, &r) is equivalent to assigning +// +// r = Result{ +// "Grace R. Emlin", // name +// "phone", // no phone given +// []Email{ +// Email{ "home", "gre@example.com" }, +// Email{ "work", "gre@work.com" } +// } +// } +// +// Note that the field r.phone has not been modified and +// that the JSON field "address" was discarded. +// +// On success, Unmarshal returns with ok set to true. +// On a syntax error, it returns with ok set to false and errtok +// set to the offending token. func Unmarshal(s string, val interface{}) (ok bool, errtok string) { var errindx int; var val1 interface{}; -- cgit v1.2.3