jsonpb.go 35 KB

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