jsonpb.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2015 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. /*
  32. Package jsonpb provides marshaling and unmarshaling between protocol buffers and JSON.
  33. It follows the specification at https://developers.google.com/protocol-buffers/docs/proto3#json.
  34. This package produces a different output than the standard "encoding/json" package,
  35. which does not operate correctly on protocol buffers.
  36. */
  37. package jsonpb
  38. import (
  39. "bytes"
  40. "encoding/json"
  41. "errors"
  42. "fmt"
  43. "io"
  44. "math"
  45. "reflect"
  46. "sort"
  47. "strconv"
  48. "strings"
  49. "time"
  50. "github.com/gogo/protobuf/proto"
  51. "github.com/gogo/protobuf/types"
  52. )
  53. // Marshaler is a configurable object for converting between
  54. // protocol buffer objects and a JSON representation for them.
  55. type Marshaler struct {
  56. // Whether to render enum values as integers, as opposed to string values.
  57. EnumsAsInts bool
  58. // Whether to render fields with zero values.
  59. EmitDefaults bool
  60. // A string to indent each level by. The presence of this field will
  61. // also cause a space to appear between the field separator and
  62. // value, and for newlines to be appear between fields and array
  63. // elements.
  64. Indent string
  65. // Whether to use the original (.proto) name for fields.
  66. OrigName bool
  67. }
  68. // JSONPBMarshaler is implemented by protobuf messages that customize the
  69. // way they are marshaled to JSON. Messages that implement this should
  70. // also implement JSONPBUnmarshaler so that the custom format can be
  71. // parsed.
  72. type JSONPBMarshaler interface {
  73. MarshalJSONPB(*Marshaler) ([]byte, error)
  74. }
  75. // JSONPBUnmarshaler is implemented by protobuf messages that customize
  76. // the way they are unmarshaled from JSON. Messages that implement this
  77. // should also implement JSONPBMarshaler so that the custom format can be
  78. // produced.
  79. type JSONPBUnmarshaler interface {
  80. UnmarshalJSONPB(*Unmarshaler, []byte) error
  81. }
  82. // Marshal marshals a protocol buffer into JSON.
  83. func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
  84. writer := &errWriter{writer: out}
  85. return m.marshalObject(writer, pb, "", "")
  86. }
  87. // MarshalToString converts a protocol buffer object to JSON string.
  88. func (m *Marshaler) MarshalToString(pb proto.Message) (string, error) {
  89. var buf bytes.Buffer
  90. if err := m.Marshal(&buf, pb); err != nil {
  91. return "", err
  92. }
  93. return buf.String(), nil
  94. }
  95. type int32Slice []int32
  96. var nonFinite = map[string]float64{
  97. `"NaN"`: math.NaN(),
  98. `"Infinity"`: math.Inf(1),
  99. `"-Infinity"`: math.Inf(-1),
  100. }
  101. // For sorting extensions ids to ensure stable output.
  102. func (s int32Slice) Len() int { return len(s) }
  103. func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
  104. func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  105. type isWkt interface {
  106. XXX_WellKnownType() string
  107. }
  108. // marshalObject writes a struct to the Writer.
  109. func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error {
  110. if jsm, ok := v.(JSONPBMarshaler); ok {
  111. b, err := jsm.MarshalJSONPB(m)
  112. if err != nil {
  113. return err
  114. }
  115. if typeURL != "" {
  116. // we are marshaling this object to an Any type
  117. var js map[string]*json.RawMessage
  118. if err = json.Unmarshal(b, &js); err != nil {
  119. return fmt.Errorf("type %T produced invalid JSON: %v", v, err)
  120. }
  121. turl, err := json.Marshal(typeURL)
  122. if err != nil {
  123. return fmt.Errorf("failed to marshal type URL %q to JSON: %v", typeURL, err)
  124. }
  125. js["@type"] = (*json.RawMessage)(&turl)
  126. if b, err = json.Marshal(js); err != nil {
  127. return err
  128. }
  129. }
  130. out.write(string(b))
  131. return out.err
  132. }
  133. s := reflect.ValueOf(v).Elem()
  134. // Handle well-known types.
  135. if wkt, ok := v.(isWkt); ok {
  136. switch wkt.XXX_WellKnownType() {
  137. case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
  138. "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
  139. // "Wrappers use the same representation in JSON
  140. // as the wrapped primitive type, ..."
  141. sprop := proto.GetProperties(s.Type())
  142. return m.marshalValue(out, sprop.Prop[0], s.Field(0), indent)
  143. case "Any":
  144. // Any is a bit more involved.
  145. return m.marshalAny(out, v, indent)
  146. case "Duration":
  147. // "Generated output always contains 3, 6, or 9 fractional digits,
  148. // depending on required precision."
  149. s, ns := s.Field(0).Int(), s.Field(1).Int()
  150. d := time.Duration(s)*time.Second + time.Duration(ns)*time.Nanosecond
  151. x := fmt.Sprintf("%.9f", d.Seconds())
  152. x = strings.TrimSuffix(x, "000")
  153. x = strings.TrimSuffix(x, "000")
  154. out.write(`"`)
  155. out.write(x)
  156. out.write(`s"`)
  157. return out.err
  158. case "Struct", "ListValue":
  159. // Let marshalValue handle the `Struct.fields` map or the `ListValue.values` slice.
  160. // TODO: pass the correct Properties if needed.
  161. return m.marshalValue(out, &proto.Properties{}, s.Field(0), indent)
  162. case "Timestamp":
  163. // "RFC 3339, where generated output will always be Z-normalized
  164. // and uses 3, 6 or 9 fractional digits."
  165. s, ns := s.Field(0).Int(), s.Field(1).Int()
  166. t := time.Unix(s, ns).UTC()
  167. // time.RFC3339Nano isn't exactly right (we need to get 3/6/9 fractional digits).
  168. x := t.Format("2006-01-02T15:04:05.000000000")
  169. x = strings.TrimSuffix(x, "000")
  170. x = strings.TrimSuffix(x, "000")
  171. out.write(`"`)
  172. out.write(x)
  173. out.write(`Z"`)
  174. return out.err
  175. case "Value":
  176. // Value has a single oneof.
  177. kind := s.Field(0)
  178. if kind.IsNil() {
  179. // "absence of any variant indicates an error"
  180. return errors.New("nil Value")
  181. }
  182. // oneof -> *T -> T -> T.F
  183. x := kind.Elem().Elem().Field(0)
  184. // TODO: pass the correct Properties if needed.
  185. return m.marshalValue(out, &proto.Properties{}, x, indent)
  186. }
  187. }
  188. out.write("{")
  189. if m.Indent != "" {
  190. out.write("\n")
  191. }
  192. firstField := true
  193. if typeURL != "" {
  194. if err := m.marshalTypeURL(out, indent, typeURL); err != nil {
  195. return err
  196. }
  197. firstField = false
  198. }
  199. for i := 0; i < s.NumField(); i++ {
  200. value := s.Field(i)
  201. valueField := s.Type().Field(i)
  202. if strings.HasPrefix(valueField.Name, "XXX_") {
  203. continue
  204. }
  205. //this is not a protobuf field
  206. if valueField.Tag.Get("protobuf") == "" && valueField.Tag.Get("protobuf_oneof") == "" {
  207. continue
  208. }
  209. // IsNil will panic on most value kinds.
  210. switch value.Kind() {
  211. case reflect.Chan, reflect.Func, reflect.Interface:
  212. if value.IsNil() {
  213. continue
  214. }
  215. }
  216. if !m.EmitDefaults {
  217. switch value.Kind() {
  218. case reflect.Bool:
  219. if !value.Bool() {
  220. continue
  221. }
  222. case reflect.Int32, reflect.Int64:
  223. if value.Int() == 0 {
  224. continue
  225. }
  226. case reflect.Uint32, reflect.Uint64:
  227. if value.Uint() == 0 {
  228. continue
  229. }
  230. case reflect.Float32, reflect.Float64:
  231. if value.Float() == 0 {
  232. continue
  233. }
  234. case reflect.String:
  235. if value.Len() == 0 {
  236. continue
  237. }
  238. case reflect.Map, reflect.Ptr, reflect.Slice:
  239. if value.IsNil() {
  240. continue
  241. }
  242. }
  243. }
  244. // Oneof fields need special handling.
  245. if valueField.Tag.Get("protobuf_oneof") != "" {
  246. // value is an interface containing &T{real_value}.
  247. sv := value.Elem().Elem() // interface -> *T -> T
  248. value = sv.Field(0)
  249. valueField = sv.Type().Field(0)
  250. }
  251. prop := jsonProperties(valueField, m.OrigName)
  252. if !firstField {
  253. m.writeSep(out)
  254. }
  255. // If the map value is a cast type, it may not implement proto.Message, therefore
  256. // allow the struct tag to declare the underlying message type. Instead of changing
  257. // the signatures of the child types (and because prop.mvalue is not public), use
  258. // CustomType as a passer.
  259. if value.Kind() == reflect.Map {
  260. if tag := valueField.Tag.Get("protobuf"); tag != "" {
  261. for _, v := range strings.Split(tag, ",") {
  262. if !strings.HasPrefix(v, "castvaluetype=") {
  263. continue
  264. }
  265. v = strings.TrimPrefix(v, "castvaluetype=")
  266. prop.CustomType = v
  267. break
  268. }
  269. }
  270. }
  271. if err := m.marshalField(out, prop, value, indent); err != nil {
  272. return err
  273. }
  274. firstField = false
  275. }
  276. // Handle proto2 extensions.
  277. if ep, ok := v.(proto.Message); ok {
  278. extensions := proto.RegisteredExtensions(v)
  279. // Sort extensions for stable output.
  280. ids := make([]int32, 0, len(extensions))
  281. for id, desc := range extensions {
  282. if !proto.HasExtension(ep, desc) {
  283. continue
  284. }
  285. ids = append(ids, id)
  286. }
  287. sort.Sort(int32Slice(ids))
  288. for _, id := range ids {
  289. desc := extensions[id]
  290. if desc == nil {
  291. // unknown extension
  292. continue
  293. }
  294. ext, extErr := proto.GetExtension(ep, desc)
  295. if extErr != nil {
  296. return extErr
  297. }
  298. value := reflect.ValueOf(ext)
  299. var prop proto.Properties
  300. prop.Parse(desc.Tag)
  301. prop.JSONName = fmt.Sprintf("[%s]", desc.Name)
  302. if !firstField {
  303. m.writeSep(out)
  304. }
  305. if err := m.marshalField(out, &prop, value, indent); err != nil {
  306. return err
  307. }
  308. firstField = false
  309. }
  310. }
  311. if m.Indent != "" {
  312. out.write("\n")
  313. out.write(indent)
  314. }
  315. out.write("}")
  316. return out.err
  317. }
  318. func (m *Marshaler) writeSep(out *errWriter) {
  319. if m.Indent != "" {
  320. out.write(",\n")
  321. } else {
  322. out.write(",")
  323. }
  324. }
  325. func (m *Marshaler) marshalAny(out *errWriter, any proto.Message, indent string) error {
  326. // "If the Any contains a value that has a special JSON mapping,
  327. // it will be converted as follows: {"@type": xxx, "value": yyy}.
  328. // Otherwise, the value will be converted into a JSON object,
  329. // and the "@type" field will be inserted to indicate the actual data type."
  330. v := reflect.ValueOf(any).Elem()
  331. turl := v.Field(0).String()
  332. val := v.Field(1).Bytes()
  333. // Only the part of type_url after the last slash is relevant.
  334. mname := turl
  335. if slash := strings.LastIndex(mname, "/"); slash >= 0 {
  336. mname = mname[slash+1:]
  337. }
  338. mt := proto.MessageType(mname)
  339. if mt == nil {
  340. return fmt.Errorf("unknown message type %q", mname)
  341. }
  342. msg := reflect.New(mt.Elem()).Interface().(proto.Message)
  343. if err := proto.Unmarshal(val, msg); err != nil {
  344. return err
  345. }
  346. if _, ok := msg.(isWkt); ok {
  347. out.write("{")
  348. if m.Indent != "" {
  349. out.write("\n")
  350. }
  351. if err := m.marshalTypeURL(out, indent, turl); err != nil {
  352. return err
  353. }
  354. m.writeSep(out)
  355. if m.Indent != "" {
  356. out.write(indent)
  357. out.write(m.Indent)
  358. out.write(`"value": `)
  359. } else {
  360. out.write(`"value":`)
  361. }
  362. if err := m.marshalObject(out, msg, indent+m.Indent, ""); err != nil {
  363. return err
  364. }
  365. if m.Indent != "" {
  366. out.write("\n")
  367. out.write(indent)
  368. }
  369. out.write("}")
  370. return out.err
  371. }
  372. return m.marshalObject(out, msg, indent, turl)
  373. }
  374. func (m *Marshaler) marshalTypeURL(out *errWriter, indent, typeURL string) error {
  375. if m.Indent != "" {
  376. out.write(indent)
  377. out.write(m.Indent)
  378. }
  379. out.write(`"@type":`)
  380. if m.Indent != "" {
  381. out.write(" ")
  382. }
  383. b, err := json.Marshal(typeURL)
  384. if err != nil {
  385. return err
  386. }
  387. out.write(string(b))
  388. return out.err
  389. }
  390. // marshalField writes field description and value to the Writer.
  391. func (m *Marshaler) marshalField(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
  392. if m.Indent != "" {
  393. out.write(indent)
  394. out.write(m.Indent)
  395. }
  396. out.write(`"`)
  397. out.write(prop.JSONName)
  398. out.write(`":`)
  399. if m.Indent != "" {
  400. out.write(" ")
  401. }
  402. if err := m.marshalValue(out, prop, v, indent); err != nil {
  403. return err
  404. }
  405. return nil
  406. }
  407. // marshalValue writes the value to the Writer.
  408. func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
  409. v = reflect.Indirect(v)
  410. // Handle nil pointer
  411. if v.Kind() == reflect.Invalid {
  412. out.write("null")
  413. return out.err
  414. }
  415. // Handle repeated elements.
  416. if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
  417. out.write("[")
  418. comma := ""
  419. for i := 0; i < v.Len(); i++ {
  420. sliceVal := v.Index(i)
  421. out.write(comma)
  422. if m.Indent != "" {
  423. out.write("\n")
  424. out.write(indent)
  425. out.write(m.Indent)
  426. out.write(m.Indent)
  427. }
  428. if err := m.marshalValue(out, prop, sliceVal, indent+m.Indent); err != nil {
  429. return err
  430. }
  431. comma = ","
  432. }
  433. if m.Indent != "" {
  434. out.write("\n")
  435. out.write(indent)
  436. out.write(m.Indent)
  437. }
  438. out.write("]")
  439. return out.err
  440. }
  441. // Handle well-known types.
  442. // Most are handled up in marshalObject (because 99% are messages).
  443. if wkt, ok := v.Interface().(isWkt); ok {
  444. switch wkt.XXX_WellKnownType() {
  445. case "NullValue":
  446. out.write("null")
  447. return out.err
  448. }
  449. }
  450. if t, ok := v.Interface().(time.Time); ok {
  451. ts, err := types.TimestampProto(t)
  452. if err != nil {
  453. return err
  454. }
  455. return m.marshalValue(out, prop, reflect.ValueOf(ts), indent)
  456. }
  457. if d, ok := v.Interface().(time.Duration); ok {
  458. dur := types.DurationProto(d)
  459. return m.marshalValue(out, prop, reflect.ValueOf(dur), indent)
  460. }
  461. // Handle enumerations.
  462. if !m.EnumsAsInts && prop.Enum != "" {
  463. // Unknown enum values will are stringified by the proto library as their
  464. // value. Such values should _not_ be quoted or they will be interpreted
  465. // as an enum string instead of their value.
  466. enumStr := v.Interface().(fmt.Stringer).String()
  467. var valStr string
  468. if v.Kind() == reflect.Ptr {
  469. valStr = strconv.Itoa(int(v.Elem().Int()))
  470. } else {
  471. valStr = strconv.Itoa(int(v.Int()))
  472. }
  473. if m, ok := v.Interface().(interface {
  474. MarshalJSON() ([]byte, error)
  475. }); ok {
  476. data, err := m.MarshalJSON()
  477. if err != nil {
  478. return err
  479. }
  480. enumStr = string(data)
  481. enumStr, err = strconv.Unquote(enumStr)
  482. if err != nil {
  483. return err
  484. }
  485. }
  486. isKnownEnum := enumStr != valStr
  487. if isKnownEnum {
  488. out.write(`"`)
  489. }
  490. out.write(enumStr)
  491. if isKnownEnum {
  492. out.write(`"`)
  493. }
  494. return out.err
  495. }
  496. // Handle nested messages.
  497. if v.Kind() == reflect.Struct {
  498. i := v
  499. if v.CanAddr() {
  500. i = v.Addr()
  501. } else {
  502. i = reflect.New(v.Type())
  503. i.Elem().Set(v)
  504. }
  505. iface := i.Interface()
  506. if iface == nil {
  507. out.write(`null`)
  508. return out.err
  509. }
  510. if m, ok := v.Interface().(interface {
  511. MarshalJSON() ([]byte, error)
  512. }); ok {
  513. data, err := m.MarshalJSON()
  514. if err != nil {
  515. return err
  516. }
  517. out.write(string(data))
  518. return nil
  519. }
  520. pm, ok := iface.(proto.Message)
  521. if !ok {
  522. if prop.CustomType == "" {
  523. return fmt.Errorf("%v does not implement proto.Message", v.Type())
  524. }
  525. t := proto.MessageType(prop.CustomType)
  526. if t == nil || !i.Type().ConvertibleTo(t) {
  527. return fmt.Errorf("%v declared custom type %s but it is not convertible to %v", v.Type(), prop.CustomType, t)
  528. }
  529. pm = i.Convert(t).Interface().(proto.Message)
  530. }
  531. return m.marshalObject(out, pm, indent+m.Indent, "")
  532. }
  533. // Handle maps.
  534. // Since Go randomizes map iteration, we sort keys for stable output.
  535. if v.Kind() == reflect.Map {
  536. out.write(`{`)
  537. keys := v.MapKeys()
  538. sort.Sort(mapKeys(keys))
  539. for i, k := range keys {
  540. if i > 0 {
  541. out.write(`,`)
  542. }
  543. if m.Indent != "" {
  544. out.write("\n")
  545. out.write(indent)
  546. out.write(m.Indent)
  547. out.write(m.Indent)
  548. }
  549. b, err := json.Marshal(k.Interface())
  550. if err != nil {
  551. return err
  552. }
  553. s := string(b)
  554. // If the JSON is not a string value, encode it again to make it one.
  555. if !strings.HasPrefix(s, `"`) {
  556. b, err := json.Marshal(s)
  557. if err != nil {
  558. return err
  559. }
  560. s = string(b)
  561. }
  562. out.write(s)
  563. out.write(`:`)
  564. if m.Indent != "" {
  565. out.write(` `)
  566. }
  567. if err := m.marshalValue(out, prop, v.MapIndex(k), indent+m.Indent); err != nil {
  568. return err
  569. }
  570. }
  571. if m.Indent != "" {
  572. out.write("\n")
  573. out.write(indent)
  574. out.write(m.Indent)
  575. }
  576. out.write(`}`)
  577. return out.err
  578. }
  579. // Handle non-finite floats, e.g. NaN, Infinity and -Infinity.
  580. if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
  581. f := v.Float()
  582. var sval string
  583. switch {
  584. case math.IsInf(f, 1):
  585. sval = `"Infinity"`
  586. case math.IsInf(f, -1):
  587. sval = `"-Infinity"`
  588. case math.IsNaN(f):
  589. sval = `"NaN"`
  590. }
  591. if sval != "" {
  592. out.write(sval)
  593. return out.err
  594. }
  595. }
  596. // Default handling defers to the encoding/json library.
  597. b, err := json.Marshal(v.Interface())
  598. if err != nil {
  599. return err
  600. }
  601. needToQuote := string(b[0]) != `"` && (v.Kind() == reflect.Int64 || v.Kind() == reflect.Uint64)
  602. if needToQuote {
  603. out.write(`"`)
  604. }
  605. out.write(string(b))
  606. if needToQuote {
  607. out.write(`"`)
  608. }
  609. return out.err
  610. }
  611. // Unmarshaler is a configurable object for converting from a JSON
  612. // representation to a protocol buffer object.
  613. type Unmarshaler struct {
  614. // Whether to allow messages to contain unknown fields, as opposed to
  615. // failing to unmarshal.
  616. AllowUnknownFields bool
  617. }
  618. // UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
  619. // This function is lenient and will decode any options permutations of the
  620. // related Marshaler.
  621. func (u *Unmarshaler) UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
  622. inputValue := json.RawMessage{}
  623. if err := dec.Decode(&inputValue); err != nil {
  624. return err
  625. }
  626. return u.unmarshalValue(reflect.ValueOf(pb).Elem(), inputValue, nil)
  627. }
  628. // Unmarshal unmarshals a JSON object stream into a protocol
  629. // buffer. This function is lenient and will decode any options
  630. // permutations of the related Marshaler.
  631. func (u *Unmarshaler) Unmarshal(r io.Reader, pb proto.Message) error {
  632. dec := json.NewDecoder(r)
  633. return u.UnmarshalNext(dec, pb)
  634. }
  635. // UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
  636. // This function is lenient and will decode any options permutations of the
  637. // related Marshaler.
  638. func UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
  639. return new(Unmarshaler).UnmarshalNext(dec, pb)
  640. }
  641. // Unmarshal unmarshals a JSON object stream into a protocol
  642. // buffer. This function is lenient and will decode any options
  643. // permutations of the related Marshaler.
  644. func Unmarshal(r io.Reader, pb proto.Message) error {
  645. return new(Unmarshaler).Unmarshal(r, pb)
  646. }
  647. // UnmarshalString will populate the fields of a protocol buffer based
  648. // on a JSON string. This function is lenient and will decode any options
  649. // permutations of the related Marshaler.
  650. func UnmarshalString(str string, pb proto.Message) error {
  651. return new(Unmarshaler).Unmarshal(strings.NewReader(str), pb)
  652. }
  653. // unmarshalValue converts/copies a value into the target.
  654. // prop may be nil.
  655. func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMessage, prop *proto.Properties) error {
  656. targetType := target.Type()
  657. // Allocate memory for pointer fields.
  658. if targetType.Kind() == reflect.Ptr {
  659. // If input value is "null" and target is a pointer type, then the field should be treated as not set
  660. // UNLESS the target is structpb.Value, in which case it should be set to structpb.NullValue.
  661. if string(inputValue) == "null" && targetType != reflect.TypeOf(&types.Value{}) {
  662. return nil
  663. }
  664. target.Set(reflect.New(targetType.Elem()))
  665. return u.unmarshalValue(target.Elem(), inputValue, prop)
  666. }
  667. if jsu, ok := target.Addr().Interface().(JSONPBUnmarshaler); ok {
  668. return jsu.UnmarshalJSONPB(u, []byte(inputValue))
  669. }
  670. // Handle well-known types that are not pointers.
  671. if w, ok := target.Addr().Interface().(isWkt); ok {
  672. switch w.XXX_WellKnownType() {
  673. case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
  674. "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
  675. return u.unmarshalValue(target.Field(0), inputValue, prop)
  676. case "Any":
  677. // Use json.RawMessage pointer type instead of value to support pre-1.8 version.
  678. // 1.8 changed RawMessage.MarshalJSON from pointer type to value type, see
  679. // https://github.com/golang/go/issues/14493
  680. var jsonFields map[string]*json.RawMessage
  681. if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
  682. return err
  683. }
  684. val, ok := jsonFields["@type"]
  685. if !ok || val == nil {
  686. return errors.New("Any JSON doesn't have '@type'")
  687. }
  688. var turl string
  689. if err := json.Unmarshal([]byte(*val), &turl); err != nil {
  690. return fmt.Errorf("can't unmarshal Any's '@type': %q", *val)
  691. }
  692. target.Field(0).SetString(turl)
  693. mname := turl
  694. if slash := strings.LastIndex(mname, "/"); slash >= 0 {
  695. mname = mname[slash+1:]
  696. }
  697. mt := proto.MessageType(mname)
  698. if mt == nil {
  699. return fmt.Errorf("unknown message type %q", mname)
  700. }
  701. m := reflect.New(mt.Elem()).Interface().(proto.Message)
  702. if _, ok := m.(isWkt); ok {
  703. val, ok := jsonFields["value"]
  704. if !ok {
  705. return errors.New("Any JSON doesn't have 'value'")
  706. }
  707. if err := u.unmarshalValue(reflect.ValueOf(m).Elem(), *val, nil); err != nil {
  708. return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err)
  709. }
  710. } else {
  711. delete(jsonFields, "@type")
  712. nestedProto, err := json.Marshal(jsonFields)
  713. if err != nil {
  714. return fmt.Errorf("can't generate JSON for Any's nested proto to be unmarshaled: %v", err)
  715. }
  716. if err = u.unmarshalValue(reflect.ValueOf(m).Elem(), nestedProto, nil); err != nil {
  717. return fmt.Errorf("can't unmarshal Any nested proto %T: %v", m, err)
  718. }
  719. }
  720. b, err := proto.Marshal(m)
  721. if err != nil {
  722. return fmt.Errorf("can't marshal proto %T into Any.Value: %v", m, err)
  723. }
  724. target.Field(1).SetBytes(b)
  725. return nil
  726. case "Duration":
  727. unq, err := strconv.Unquote(string(inputValue))
  728. if err != nil {
  729. return err
  730. }
  731. d, err := time.ParseDuration(unq)
  732. if err != nil {
  733. return fmt.Errorf("bad Duration: %v", err)
  734. }
  735. ns := d.Nanoseconds()
  736. s := ns / 1e9
  737. ns %= 1e9
  738. target.Field(0).SetInt(s)
  739. target.Field(1).SetInt(ns)
  740. return nil
  741. case "Timestamp":
  742. unq, err := strconv.Unquote(string(inputValue))
  743. if err != nil {
  744. return err
  745. }
  746. t, err := time.Parse(time.RFC3339Nano, unq)
  747. if err != nil {
  748. return fmt.Errorf("bad Timestamp: %v", err)
  749. }
  750. target.Field(0).SetInt(t.Unix())
  751. target.Field(1).SetInt(int64(t.Nanosecond()))
  752. return nil
  753. case "Struct":
  754. var m map[string]json.RawMessage
  755. if err := json.Unmarshal(inputValue, &m); err != nil {
  756. return fmt.Errorf("bad StructValue: %v", err)
  757. }
  758. target.Field(0).Set(reflect.ValueOf(map[string]*types.Value{}))
  759. for k, jv := range m {
  760. pv := &types.Value{}
  761. if err := u.unmarshalValue(reflect.ValueOf(pv).Elem(), jv, prop); err != nil {
  762. return fmt.Errorf("bad value in StructValue for key %q: %v", k, err)
  763. }
  764. target.Field(0).SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(pv))
  765. }
  766. return nil
  767. case "ListValue":
  768. var s []json.RawMessage
  769. if err := json.Unmarshal(inputValue, &s); err != nil {
  770. return fmt.Errorf("bad ListValue: %v", err)
  771. }
  772. target.Field(0).Set(reflect.ValueOf(make([]*types.Value, len(s), len(s))))
  773. for i, sv := range s {
  774. if err := u.unmarshalValue(target.Field(0).Index(i), sv, prop); err != nil {
  775. return err
  776. }
  777. }
  778. return nil
  779. case "Value":
  780. ivStr := string(inputValue)
  781. if ivStr == "null" {
  782. target.Field(0).Set(reflect.ValueOf(&types.Value_NullValue{}))
  783. } else if v, err := strconv.ParseFloat(ivStr, 0); err == nil {
  784. target.Field(0).Set(reflect.ValueOf(&types.Value_NumberValue{NumberValue: v}))
  785. } else if v, err := strconv.Unquote(ivStr); err == nil {
  786. target.Field(0).Set(reflect.ValueOf(&types.Value_StringValue{StringValue: v}))
  787. } else if v, err := strconv.ParseBool(ivStr); err == nil {
  788. target.Field(0).Set(reflect.ValueOf(&types.Value_BoolValue{BoolValue: v}))
  789. } else if err := json.Unmarshal(inputValue, &[]json.RawMessage{}); err == nil {
  790. lv := &types.ListValue{}
  791. target.Field(0).Set(reflect.ValueOf(&types.Value_ListValue{ListValue: lv}))
  792. return u.unmarshalValue(reflect.ValueOf(lv).Elem(), inputValue, prop)
  793. } else if err := json.Unmarshal(inputValue, &map[string]json.RawMessage{}); err == nil {
  794. sv := &types.Struct{}
  795. target.Field(0).Set(reflect.ValueOf(&types.Value_StructValue{StructValue: sv}))
  796. return u.unmarshalValue(reflect.ValueOf(sv).Elem(), inputValue, prop)
  797. } else {
  798. return fmt.Errorf("unrecognized type for Value %q", ivStr)
  799. }
  800. return nil
  801. }
  802. }
  803. if t, ok := target.Addr().Interface().(*time.Time); ok {
  804. ts := &types.Timestamp{}
  805. if err := u.unmarshalValue(reflect.ValueOf(ts).Elem(), inputValue, prop); err != nil {
  806. return err
  807. }
  808. tt, err := types.TimestampFromProto(ts)
  809. if err != nil {
  810. return err
  811. }
  812. *t = tt
  813. return nil
  814. }
  815. if d, ok := target.Addr().Interface().(*time.Duration); ok {
  816. dur := &types.Duration{}
  817. if err := u.unmarshalValue(reflect.ValueOf(dur).Elem(), inputValue, prop); err != nil {
  818. return err
  819. }
  820. dd, err := types.DurationFromProto(dur)
  821. if err != nil {
  822. return err
  823. }
  824. *d = dd
  825. return nil
  826. }
  827. // Handle enums, which have an underlying type of int32,
  828. // and may appear as strings.
  829. // The case of an enum appearing as a number is handled
  830. // at the bottom of this function.
  831. if inputValue[0] == '"' && prop != nil && prop.Enum != "" {
  832. vmap := proto.EnumValueMap(prop.Enum)
  833. // Don't need to do unquoting; valid enum names
  834. // are from a limited character set.
  835. s := inputValue[1 : len(inputValue)-1]
  836. n, ok := vmap[string(s)]
  837. if !ok {
  838. return fmt.Errorf("unknown value %q for enum %s", s, prop.Enum)
  839. }
  840. if target.Kind() == reflect.Ptr { // proto2
  841. target.Set(reflect.New(targetType.Elem()))
  842. target = target.Elem()
  843. }
  844. target.SetInt(int64(n))
  845. return nil
  846. }
  847. // Handle nested messages.
  848. if targetType.Kind() == reflect.Struct {
  849. if prop != nil && len(prop.CustomType) > 0 && target.CanAddr() {
  850. if m, ok := target.Addr().Interface().(interface {
  851. UnmarshalJSON([]byte) error
  852. }); ok {
  853. return json.Unmarshal(inputValue, m)
  854. }
  855. }
  856. var jsonFields map[string]json.RawMessage
  857. if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
  858. return err
  859. }
  860. consumeField := func(prop *proto.Properties) (json.RawMessage, bool) {
  861. // Be liberal in what names we accept; both orig_name and camelName are okay.
  862. fieldNames := acceptedJSONFieldNames(prop)
  863. vOrig, okOrig := jsonFields[fieldNames.orig]
  864. vCamel, okCamel := jsonFields[fieldNames.camel]
  865. if !okOrig && !okCamel {
  866. return nil, false
  867. }
  868. // If, for some reason, both are present in the data, favour the camelName.
  869. var raw json.RawMessage
  870. if okOrig {
  871. raw = vOrig
  872. delete(jsonFields, fieldNames.orig)
  873. }
  874. if okCamel {
  875. raw = vCamel
  876. delete(jsonFields, fieldNames.camel)
  877. }
  878. return raw, true
  879. }
  880. sprops := proto.GetProperties(targetType)
  881. for i := 0; i < target.NumField(); i++ {
  882. ft := target.Type().Field(i)
  883. if strings.HasPrefix(ft.Name, "XXX_") {
  884. continue
  885. }
  886. valueForField, ok := consumeField(sprops.Prop[i])
  887. if !ok {
  888. continue
  889. }
  890. if err := u.unmarshalValue(target.Field(i), valueForField, sprops.Prop[i]); err != nil {
  891. return err
  892. }
  893. }
  894. // Check for any oneof fields.
  895. if len(jsonFields) > 0 {
  896. for _, oop := range sprops.OneofTypes {
  897. raw, ok := consumeField(oop.Prop)
  898. if !ok {
  899. continue
  900. }
  901. nv := reflect.New(oop.Type.Elem())
  902. target.Field(oop.Field).Set(nv)
  903. if err := u.unmarshalValue(nv.Elem().Field(0), raw, oop.Prop); err != nil {
  904. return err
  905. }
  906. }
  907. }
  908. // Handle proto2 extensions.
  909. if len(jsonFields) > 0 {
  910. if ep, ok := target.Addr().Interface().(proto.Message); ok {
  911. for _, ext := range proto.RegisteredExtensions(ep) {
  912. name := fmt.Sprintf("[%s]", ext.Name)
  913. raw, ok := jsonFields[name]
  914. if !ok {
  915. continue
  916. }
  917. delete(jsonFields, name)
  918. nv := reflect.New(reflect.TypeOf(ext.ExtensionType).Elem())
  919. if err := u.unmarshalValue(nv.Elem(), raw, nil); err != nil {
  920. return err
  921. }
  922. if err := proto.SetExtension(ep, ext, nv.Interface()); err != nil {
  923. return err
  924. }
  925. }
  926. }
  927. }
  928. if !u.AllowUnknownFields && len(jsonFields) > 0 {
  929. // Pick any field to be the scapegoat.
  930. var f string
  931. for fname := range jsonFields {
  932. f = fname
  933. break
  934. }
  935. return fmt.Errorf("unknown field %q in %v", f, targetType)
  936. }
  937. return nil
  938. }
  939. // Handle arrays
  940. if targetType.Kind() == reflect.Slice {
  941. if targetType.Elem().Kind() == reflect.Uint8 {
  942. outRef := reflect.New(targetType)
  943. outVal := outRef.Interface()
  944. //CustomType with underlying type []byte
  945. if _, ok := outVal.(interface {
  946. UnmarshalJSON([]byte) error
  947. }); ok {
  948. if err := json.Unmarshal(inputValue, outVal); err != nil {
  949. return err
  950. }
  951. target.Set(outRef.Elem())
  952. return nil
  953. }
  954. // Special case for encoded bytes. Pre-go1.5 doesn't support unmarshalling
  955. // strings into aliased []byte types.
  956. // https://github.com/golang/go/commit/4302fd0409da5e4f1d71471a6770dacdc3301197
  957. // https://github.com/golang/go/commit/c60707b14d6be26bf4213114d13070bff00d0b0a
  958. var out []byte
  959. if err := json.Unmarshal(inputValue, &out); err != nil {
  960. return err
  961. }
  962. target.SetBytes(out)
  963. return nil
  964. }
  965. var slc []json.RawMessage
  966. if err := json.Unmarshal(inputValue, &slc); err != nil {
  967. return err
  968. }
  969. if slc != nil {
  970. l := len(slc)
  971. target.Set(reflect.MakeSlice(targetType, l, l))
  972. for i := 0; i < l; i++ {
  973. if err := u.unmarshalValue(target.Index(i), slc[i], prop); err != nil {
  974. return err
  975. }
  976. }
  977. }
  978. return nil
  979. }
  980. // Handle maps (whose keys are always strings)
  981. if targetType.Kind() == reflect.Map {
  982. var mp map[string]json.RawMessage
  983. if err := json.Unmarshal(inputValue, &mp); err != nil {
  984. return err
  985. }
  986. if mp != nil {
  987. target.Set(reflect.MakeMap(targetType))
  988. var keyprop, valprop *proto.Properties
  989. if prop != nil {
  990. // These could still be nil if the protobuf metadata is broken somehow.
  991. // TODO: This won't work because the fields are unexported.
  992. // We should probably just reparse them.
  993. //keyprop, valprop = prop.mkeyprop, prop.mvalprop
  994. }
  995. for ks, raw := range mp {
  996. // Unmarshal map key. The core json library already decoded the key into a
  997. // string, so we handle that specially. Other types were quoted post-serialization.
  998. var k reflect.Value
  999. if targetType.Key().Kind() == reflect.String {
  1000. k = reflect.ValueOf(ks)
  1001. } else {
  1002. k = reflect.New(targetType.Key()).Elem()
  1003. if err := u.unmarshalValue(k, json.RawMessage(ks), keyprop); err != nil {
  1004. return err
  1005. }
  1006. }
  1007. if !k.Type().AssignableTo(targetType.Key()) {
  1008. k = k.Convert(targetType.Key())
  1009. }
  1010. // Unmarshal map value.
  1011. v := reflect.New(targetType.Elem()).Elem()
  1012. if err := u.unmarshalValue(v, raw, valprop); err != nil {
  1013. return err
  1014. }
  1015. target.SetMapIndex(k, v)
  1016. }
  1017. }
  1018. return nil
  1019. }
  1020. // 64-bit integers can be encoded as strings. In this case we drop
  1021. // the quotes and proceed as normal.
  1022. isNum := targetType.Kind() == reflect.Int64 || targetType.Kind() == reflect.Uint64
  1023. if isNum && strings.HasPrefix(string(inputValue), `"`) {
  1024. inputValue = inputValue[1 : len(inputValue)-1]
  1025. }
  1026. // Non-finite numbers can be encoded as strings.
  1027. isFloat := targetType.Kind() == reflect.Float32 || targetType.Kind() == reflect.Float64
  1028. if isFloat {
  1029. if num, ok := nonFinite[string(inputValue)]; ok {
  1030. target.SetFloat(num)
  1031. return nil
  1032. }
  1033. }
  1034. // Use the encoding/json for parsing other value types.
  1035. return json.Unmarshal(inputValue, target.Addr().Interface())
  1036. }
  1037. // jsonProperties returns parsed proto.Properties for the field and corrects JSONName attribute.
  1038. func jsonProperties(f reflect.StructField, origName bool) *proto.Properties {
  1039. var prop proto.Properties
  1040. prop.Init(f.Type, f.Name, f.Tag.Get("protobuf"), &f)
  1041. if origName || prop.JSONName == "" {
  1042. prop.JSONName = prop.OrigName
  1043. }
  1044. return &prop
  1045. }
  1046. type fieldNames struct {
  1047. orig, camel string
  1048. }
  1049. func acceptedJSONFieldNames(prop *proto.Properties) fieldNames {
  1050. opts := fieldNames{orig: prop.OrigName, camel: prop.OrigName}
  1051. if prop.JSONName != "" {
  1052. opts.camel = prop.JSONName
  1053. }
  1054. return opts
  1055. }
  1056. // Writer wrapper inspired by https://blog.golang.org/errors-are-values
  1057. type errWriter struct {
  1058. writer io.Writer
  1059. err error
  1060. }
  1061. func (w *errWriter) write(str string) {
  1062. if w.err != nil {
  1063. return
  1064. }
  1065. _, w.err = w.writer.Write([]byte(str))
  1066. }
  1067. // Map fields may have key types of non-float scalars, strings and enums.
  1068. // The easiest way to sort them in some deterministic order is to use fmt.
  1069. // If this turns out to be inefficient we can always consider other options,
  1070. // such as doing a Schwartzian transform.
  1071. //
  1072. // Numeric keys are sorted in numeric order per
  1073. // https://developers.google.com/protocol-buffers/docs/proto#maps.
  1074. type mapKeys []reflect.Value
  1075. func (s mapKeys) Len() int { return len(s) }
  1076. func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  1077. func (s mapKeys) Less(i, j int) bool {
  1078. if k := s[i].Kind(); k == s[j].Kind() {
  1079. switch k {
  1080. case reflect.Int32, reflect.Int64:
  1081. return s[i].Int() < s[j].Int()
  1082. case reflect.Uint32, reflect.Uint64:
  1083. return s[i].Uint() < s[j].Uint()
  1084. }
  1085. }
  1086. return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface())
  1087. }