jsonpb.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. "reflect"
  45. "sort"
  46. "strconv"
  47. "strings"
  48. "time"
  49. "github.com/golang/protobuf/proto"
  50. )
  51. // Marshaler is a configurable object for converting between
  52. // protocol buffer objects and a JSON representation for them.
  53. type Marshaler struct {
  54. // Whether to render enum values as integers, as opposed to string values.
  55. EnumsAsInts bool
  56. // Whether to render fields with zero values.
  57. EmitDefaults bool
  58. // A string to indent each level by. The presence of this field will
  59. // also cause a space to appear between the field separator and
  60. // value, and for newlines to be appear between fields and array
  61. // elements.
  62. Indent string
  63. // Whether to use the original (.proto) name for fields.
  64. OrigName bool
  65. }
  66. // Marshal marshals a protocol buffer into JSON.
  67. func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
  68. writer := &errWriter{writer: out}
  69. return m.marshalObject(writer, pb, "", "")
  70. }
  71. // MarshalToString converts a protocol buffer object to JSON string.
  72. func (m *Marshaler) MarshalToString(pb proto.Message) (string, error) {
  73. var buf bytes.Buffer
  74. if err := m.Marshal(&buf, pb); err != nil {
  75. return "", err
  76. }
  77. return buf.String(), nil
  78. }
  79. type int32Slice []int32
  80. // For sorting extensions ids to ensure stable output.
  81. func (s int32Slice) Len() int { return len(s) }
  82. func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
  83. func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  84. type wkt interface {
  85. XXX_WellKnownType() string
  86. }
  87. // marshalObject writes a struct to the Writer.
  88. func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error {
  89. s := reflect.ValueOf(v).Elem()
  90. // Handle well-known types.
  91. if wkt, ok := v.(wkt); ok {
  92. switch wkt.XXX_WellKnownType() {
  93. case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
  94. "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
  95. // "Wrappers use the same representation in JSON
  96. // as the wrapped primitive type, ..."
  97. sprop := proto.GetProperties(s.Type())
  98. return m.marshalValue(out, sprop.Prop[0], s.Field(0), indent)
  99. case "Any":
  100. // Any is a bit more involved.
  101. return m.marshalAny(out, v, indent)
  102. case "Duration":
  103. // "Generated output always contains 3, 6, or 9 fractional digits,
  104. // depending on required precision."
  105. s, ns := s.Field(0).Int(), s.Field(1).Int()
  106. d := time.Duration(s)*time.Second + time.Duration(ns)*time.Nanosecond
  107. x := fmt.Sprintf("%.9f", d.Seconds())
  108. x = strings.TrimSuffix(x, "000")
  109. x = strings.TrimSuffix(x, "000")
  110. out.write(`"`)
  111. out.write(x)
  112. out.write(`s"`)
  113. return out.err
  114. case "Struct":
  115. // Let marshalValue handle the `fields` map.
  116. // TODO: pass the correct Properties if needed.
  117. return m.marshalValue(out, &proto.Properties{}, s.Field(0), indent)
  118. case "Timestamp":
  119. // "RFC 3339, where generated output will always be Z-normalized
  120. // and uses 3, 6 or 9 fractional digits."
  121. s, ns := s.Field(0).Int(), s.Field(1).Int()
  122. t := time.Unix(s, ns).UTC()
  123. // time.RFC3339Nano isn't exactly right (we need to get 3/6/9 fractional digits).
  124. x := t.Format("2006-01-02T15:04:05.000000000")
  125. x = strings.TrimSuffix(x, "000")
  126. x = strings.TrimSuffix(x, "000")
  127. out.write(`"`)
  128. out.write(x)
  129. out.write(`Z"`)
  130. return out.err
  131. case "Value":
  132. // Value has a single oneof.
  133. kind := s.Field(0)
  134. if kind.IsNil() {
  135. // "absence of any variant indicates an error"
  136. return errors.New("nil Value")
  137. }
  138. // oneof -> *T -> T -> T.F
  139. x := kind.Elem().Elem().Field(0)
  140. // TODO: pass the correct Properties if needed.
  141. return m.marshalValue(out, &proto.Properties{}, x, indent)
  142. }
  143. }
  144. out.write("{")
  145. if m.Indent != "" {
  146. out.write("\n")
  147. }
  148. firstField := true
  149. if typeURL != "" {
  150. if err := m.marshalTypeURL(out, indent, typeURL); err != nil {
  151. return err
  152. }
  153. firstField = false
  154. }
  155. for i := 0; i < s.NumField(); i++ {
  156. value := s.Field(i)
  157. valueField := s.Type().Field(i)
  158. if strings.HasPrefix(valueField.Name, "XXX_") {
  159. continue
  160. }
  161. // IsNil will panic on most value kinds.
  162. switch value.Kind() {
  163. case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  164. if value.IsNil() {
  165. continue
  166. }
  167. }
  168. if !m.EmitDefaults {
  169. switch value.Kind() {
  170. case reflect.Bool:
  171. if !value.Bool() {
  172. continue
  173. }
  174. case reflect.Int32, reflect.Int64:
  175. if value.Int() == 0 {
  176. continue
  177. }
  178. case reflect.Uint32, reflect.Uint64:
  179. if value.Uint() == 0 {
  180. continue
  181. }
  182. case reflect.Float32, reflect.Float64:
  183. if value.Float() == 0 {
  184. continue
  185. }
  186. case reflect.String:
  187. if value.Len() == 0 {
  188. continue
  189. }
  190. }
  191. }
  192. // Oneof fields need special handling.
  193. if valueField.Tag.Get("protobuf_oneof") != "" {
  194. // value is an interface containing &T{real_value}.
  195. sv := value.Elem().Elem() // interface -> *T -> T
  196. value = sv.Field(0)
  197. valueField = sv.Type().Field(0)
  198. }
  199. prop := jsonProperties(valueField, m.OrigName)
  200. if !firstField {
  201. m.writeSep(out)
  202. }
  203. if err := m.marshalField(out, prop, value, indent); err != nil {
  204. return err
  205. }
  206. firstField = false
  207. }
  208. // Handle proto2 extensions.
  209. if ep, ok := v.(proto.Message); ok {
  210. extensions := proto.RegisteredExtensions(v)
  211. // Sort extensions for stable output.
  212. ids := make([]int32, 0, len(extensions))
  213. for id, desc := range extensions {
  214. if !proto.HasExtension(ep, desc) {
  215. continue
  216. }
  217. ids = append(ids, id)
  218. }
  219. sort.Sort(int32Slice(ids))
  220. for _, id := range ids {
  221. desc := extensions[id]
  222. if desc == nil {
  223. // unknown extension
  224. continue
  225. }
  226. ext, extErr := proto.GetExtension(ep, desc)
  227. if extErr != nil {
  228. return extErr
  229. }
  230. value := reflect.ValueOf(ext)
  231. var prop proto.Properties
  232. prop.Parse(desc.Tag)
  233. prop.JSONName = fmt.Sprintf("[%s]", desc.Name)
  234. if !firstField {
  235. m.writeSep(out)
  236. }
  237. if err := m.marshalField(out, &prop, value, indent); err != nil {
  238. return err
  239. }
  240. firstField = false
  241. }
  242. }
  243. if m.Indent != "" {
  244. out.write("\n")
  245. out.write(indent)
  246. }
  247. out.write("}")
  248. return out.err
  249. }
  250. func (m *Marshaler) writeSep(out *errWriter) {
  251. if m.Indent != "" {
  252. out.write(",\n")
  253. } else {
  254. out.write(",")
  255. }
  256. }
  257. func (m *Marshaler) marshalAny(out *errWriter, any proto.Message, indent string) error {
  258. // "If the Any contains a value that has a special JSON mapping,
  259. // it will be converted as follows: {"@type": xxx, "value": yyy}.
  260. // Otherwise, the value will be converted into a JSON object,
  261. // and the "@type" field will be inserted to indicate the actual data type."
  262. v := reflect.ValueOf(any).Elem()
  263. turl := v.Field(0).String()
  264. val := v.Field(1).Bytes()
  265. // Only the part of type_url after the last slash is relevant.
  266. mname := turl
  267. if slash := strings.LastIndex(mname, "/"); slash >= 0 {
  268. mname = mname[slash+1:]
  269. }
  270. mt := proto.MessageType(mname)
  271. if mt == nil {
  272. return fmt.Errorf("unknown message type %q", mname)
  273. }
  274. msg := reflect.New(mt.Elem()).Interface().(proto.Message)
  275. if err := proto.Unmarshal(val, msg); err != nil {
  276. return err
  277. }
  278. if _, ok := msg.(wkt); ok {
  279. out.write("{")
  280. if m.Indent != "" {
  281. out.write("\n")
  282. }
  283. if err := m.marshalTypeURL(out, indent, turl); err != nil {
  284. return err
  285. }
  286. m.writeSep(out)
  287. if m.Indent != "" {
  288. out.write(indent)
  289. out.write(m.Indent)
  290. out.write(`"value": `)
  291. } else {
  292. out.write(`"value":`)
  293. }
  294. if err := m.marshalObject(out, msg, indent+m.Indent, ""); err != nil {
  295. return err
  296. }
  297. if m.Indent != "" {
  298. out.write("\n")
  299. out.write(indent)
  300. }
  301. out.write("}")
  302. return out.err
  303. }
  304. return m.marshalObject(out, msg, indent, turl)
  305. }
  306. func (m *Marshaler) marshalTypeURL(out *errWriter, indent, typeURL string) error {
  307. if m.Indent != "" {
  308. out.write(indent)
  309. out.write(m.Indent)
  310. }
  311. out.write(`"@type":`)
  312. if m.Indent != "" {
  313. out.write(" ")
  314. }
  315. b, err := json.Marshal(typeURL)
  316. if err != nil {
  317. return err
  318. }
  319. out.write(string(b))
  320. return out.err
  321. }
  322. // marshalField writes field description and value to the Writer.
  323. func (m *Marshaler) marshalField(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
  324. if m.Indent != "" {
  325. out.write(indent)
  326. out.write(m.Indent)
  327. }
  328. out.write(`"`)
  329. out.write(prop.JSONName)
  330. out.write(`":`)
  331. if m.Indent != "" {
  332. out.write(" ")
  333. }
  334. if err := m.marshalValue(out, prop, v, indent); err != nil {
  335. return err
  336. }
  337. return nil
  338. }
  339. // marshalValue writes the value to the Writer.
  340. func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
  341. var err error
  342. v = reflect.Indirect(v)
  343. // Handle repeated elements.
  344. if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
  345. out.write("[")
  346. comma := ""
  347. for i := 0; i < v.Len(); i++ {
  348. sliceVal := v.Index(i)
  349. out.write(comma)
  350. if m.Indent != "" {
  351. out.write("\n")
  352. out.write(indent)
  353. out.write(m.Indent)
  354. out.write(m.Indent)
  355. }
  356. if err := m.marshalValue(out, prop, sliceVal, indent+m.Indent); err != nil {
  357. return err
  358. }
  359. comma = ","
  360. }
  361. if m.Indent != "" {
  362. out.write("\n")
  363. out.write(indent)
  364. out.write(m.Indent)
  365. }
  366. out.write("]")
  367. return out.err
  368. }
  369. // Handle well-known types.
  370. // Most are handled up in marshalObject (because 99% are messages).
  371. type wkt interface {
  372. XXX_WellKnownType() string
  373. }
  374. if wkt, ok := v.Interface().(wkt); ok {
  375. switch wkt.XXX_WellKnownType() {
  376. case "NullValue":
  377. out.write("null")
  378. return out.err
  379. }
  380. }
  381. // Handle enumerations.
  382. if !m.EnumsAsInts && prop.Enum != "" {
  383. // Unknown enum values will are stringified by the proto library as their
  384. // value. Such values should _not_ be quoted or they will be interpreted
  385. // as an enum string instead of their value.
  386. enumStr := v.Interface().(fmt.Stringer).String()
  387. var valStr string
  388. if v.Kind() == reflect.Ptr {
  389. valStr = strconv.Itoa(int(v.Elem().Int()))
  390. } else {
  391. valStr = strconv.Itoa(int(v.Int()))
  392. }
  393. isKnownEnum := enumStr != valStr
  394. if isKnownEnum {
  395. out.write(`"`)
  396. }
  397. out.write(enumStr)
  398. if isKnownEnum {
  399. out.write(`"`)
  400. }
  401. return out.err
  402. }
  403. // Handle nested messages.
  404. if v.Kind() == reflect.Struct {
  405. return m.marshalObject(out, v.Addr().Interface().(proto.Message), indent+m.Indent, "")
  406. }
  407. // Handle maps.
  408. // Since Go randomizes map iteration, we sort keys for stable output.
  409. if v.Kind() == reflect.Map {
  410. out.write(`{`)
  411. keys := v.MapKeys()
  412. sort.Sort(mapKeys(keys))
  413. for i, k := range keys {
  414. if i > 0 {
  415. out.write(`,`)
  416. }
  417. if m.Indent != "" {
  418. out.write("\n")
  419. out.write(indent)
  420. out.write(m.Indent)
  421. out.write(m.Indent)
  422. }
  423. b, err := json.Marshal(k.Interface())
  424. if err != nil {
  425. return err
  426. }
  427. s := string(b)
  428. // If the JSON is not a string value, encode it again to make it one.
  429. if !strings.HasPrefix(s, `"`) {
  430. b, err := json.Marshal(s)
  431. if err != nil {
  432. return err
  433. }
  434. s = string(b)
  435. }
  436. out.write(s)
  437. out.write(`:`)
  438. if m.Indent != "" {
  439. out.write(` `)
  440. }
  441. if err := m.marshalValue(out, prop, v.MapIndex(k), indent+m.Indent); err != nil {
  442. return err
  443. }
  444. }
  445. if m.Indent != "" {
  446. out.write("\n")
  447. out.write(indent)
  448. out.write(m.Indent)
  449. }
  450. out.write(`}`)
  451. return out.err
  452. }
  453. // Default handling defers to the encoding/json library.
  454. b, err := json.Marshal(v.Interface())
  455. if err != nil {
  456. return err
  457. }
  458. needToQuote := string(b[0]) != `"` && (v.Kind() == reflect.Int64 || v.Kind() == reflect.Uint64)
  459. if needToQuote {
  460. out.write(`"`)
  461. }
  462. out.write(string(b))
  463. if needToQuote {
  464. out.write(`"`)
  465. }
  466. return out.err
  467. }
  468. // UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
  469. // This function is lenient and will decode any options permutations of the
  470. // related Marshaler.
  471. func UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
  472. inputValue := json.RawMessage{}
  473. if err := dec.Decode(&inputValue); err != nil {
  474. return err
  475. }
  476. return unmarshalValue(reflect.ValueOf(pb).Elem(), inputValue, nil)
  477. }
  478. // Unmarshal unmarshals a JSON object stream into a protocol
  479. // buffer. This function is lenient and will decode any options
  480. // permutations of the related Marshaler.
  481. func Unmarshal(r io.Reader, pb proto.Message) error {
  482. dec := json.NewDecoder(r)
  483. return UnmarshalNext(dec, pb)
  484. }
  485. // UnmarshalString will populate the fields of a protocol buffer based
  486. // on a JSON string. This function is lenient and will decode any options
  487. // permutations of the related Marshaler.
  488. func UnmarshalString(str string, pb proto.Message) error {
  489. return Unmarshal(strings.NewReader(str), pb)
  490. }
  491. // unmarshalValue converts/copies a value into the target.
  492. // prop may be nil.
  493. func unmarshalValue(target reflect.Value, inputValue json.RawMessage, prop *proto.Properties) error {
  494. targetType := target.Type()
  495. // Allocate memory for pointer fields.
  496. if targetType.Kind() == reflect.Ptr {
  497. target.Set(reflect.New(targetType.Elem()))
  498. return unmarshalValue(target.Elem(), inputValue, prop)
  499. }
  500. // Handle well-known types.
  501. type wkt interface {
  502. XXX_WellKnownType() string
  503. }
  504. if wkt, ok := target.Addr().Interface().(wkt); ok {
  505. switch wkt.XXX_WellKnownType() {
  506. case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
  507. "Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
  508. // "Wrappers use the same representation in JSON
  509. // as the wrapped primitive type, except that null is allowed."
  510. // encoding/json will turn JSON `null` into Go `nil`,
  511. // so we don't have to do any extra work.
  512. return unmarshalValue(target.Field(0), inputValue, prop)
  513. case "Any":
  514. return fmt.Errorf("unmarshaling Any not supported yet")
  515. case "Duration":
  516. unq, err := strconv.Unquote(string(inputValue))
  517. if err != nil {
  518. return err
  519. }
  520. d, err := time.ParseDuration(unq)
  521. if err != nil {
  522. return fmt.Errorf("bad Duration: %v", err)
  523. }
  524. ns := d.Nanoseconds()
  525. s := ns / 1e9
  526. ns %= 1e9
  527. target.Field(0).SetInt(s)
  528. target.Field(1).SetInt(ns)
  529. return nil
  530. case "Timestamp":
  531. unq, err := strconv.Unquote(string(inputValue))
  532. if err != nil {
  533. return err
  534. }
  535. t, err := time.Parse(time.RFC3339Nano, unq)
  536. if err != nil {
  537. return fmt.Errorf("bad Timestamp: %v", err)
  538. }
  539. ns := t.UnixNano()
  540. s := ns / 1e9
  541. ns %= 1e9
  542. target.Field(0).SetInt(s)
  543. target.Field(1).SetInt(ns)
  544. return nil
  545. }
  546. }
  547. // Handle enums, which have an underlying type of int32,
  548. // and may appear as strings.
  549. // The case of an enum appearing as a number is handled
  550. // at the bottom of this function.
  551. if inputValue[0] == '"' && prop != nil && prop.Enum != "" {
  552. vmap := proto.EnumValueMap(prop.Enum)
  553. // Don't need to do unquoting; valid enum names
  554. // are from a limited character set.
  555. s := inputValue[1 : len(inputValue)-1]
  556. n, ok := vmap[string(s)]
  557. if !ok {
  558. return fmt.Errorf("unknown value %q for enum %s", s, prop.Enum)
  559. }
  560. if target.Kind() == reflect.Ptr { // proto2
  561. target.Set(reflect.New(targetType.Elem()))
  562. target = target.Elem()
  563. }
  564. target.SetInt(int64(n))
  565. return nil
  566. }
  567. // Handle nested messages.
  568. if targetType.Kind() == reflect.Struct {
  569. var jsonFields map[string]json.RawMessage
  570. if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
  571. return err
  572. }
  573. consumeField := func(prop *proto.Properties) (json.RawMessage, bool) {
  574. // Be liberal in what names we accept; both orig_name and camelName are okay.
  575. fieldNames := acceptedJSONFieldNames(prop)
  576. vOrig, okOrig := jsonFields[fieldNames.orig]
  577. vCamel, okCamel := jsonFields[fieldNames.camel]
  578. if !okOrig && !okCamel {
  579. return nil, false
  580. }
  581. // If, for some reason, both are present in the data, favour the camelName.
  582. var raw json.RawMessage
  583. if okOrig {
  584. raw = vOrig
  585. delete(jsonFields, fieldNames.orig)
  586. }
  587. if okCamel {
  588. raw = vCamel
  589. delete(jsonFields, fieldNames.camel)
  590. }
  591. return raw, true
  592. }
  593. sprops := proto.GetProperties(targetType)
  594. for i := 0; i < target.NumField(); i++ {
  595. ft := target.Type().Field(i)
  596. if strings.HasPrefix(ft.Name, "XXX_") {
  597. continue
  598. }
  599. valueForField, ok := consumeField(sprops.Prop[i])
  600. if !ok {
  601. continue
  602. }
  603. if err := unmarshalValue(target.Field(i), valueForField, sprops.Prop[i]); err != nil {
  604. return err
  605. }
  606. }
  607. // Check for any oneof fields.
  608. if len(jsonFields) > 0 {
  609. for _, oop := range sprops.OneofTypes {
  610. raw, ok := consumeField(oop.Prop)
  611. if !ok {
  612. continue
  613. }
  614. nv := reflect.New(oop.Type.Elem())
  615. target.Field(oop.Field).Set(nv)
  616. if err := unmarshalValue(nv.Elem().Field(0), raw, oop.Prop); err != nil {
  617. return err
  618. }
  619. }
  620. }
  621. if len(jsonFields) > 0 {
  622. // Pick any field to be the scapegoat.
  623. var f string
  624. for fname := range jsonFields {
  625. f = fname
  626. break
  627. }
  628. return fmt.Errorf("unknown field %q in %v", f, targetType)
  629. }
  630. return nil
  631. }
  632. // Handle arrays (which aren't encoded bytes)
  633. if targetType.Kind() == reflect.Slice && targetType.Elem().Kind() != reflect.Uint8 {
  634. var slc []json.RawMessage
  635. if err := json.Unmarshal(inputValue, &slc); err != nil {
  636. return err
  637. }
  638. len := len(slc)
  639. target.Set(reflect.MakeSlice(targetType, len, len))
  640. for i := 0; i < len; i++ {
  641. if err := unmarshalValue(target.Index(i), slc[i], prop); err != nil {
  642. return err
  643. }
  644. }
  645. return nil
  646. }
  647. // Handle maps (whose keys are always strings)
  648. if targetType.Kind() == reflect.Map {
  649. var mp map[string]json.RawMessage
  650. if err := json.Unmarshal(inputValue, &mp); err != nil {
  651. return err
  652. }
  653. target.Set(reflect.MakeMap(targetType))
  654. var keyprop, valprop *proto.Properties
  655. if prop != nil {
  656. // These could still be nil if the protobuf metadata is broken somehow.
  657. // TODO: This won't work because the fields are unexported.
  658. // We should probably just reparse them.
  659. //keyprop, valprop = prop.mkeyprop, prop.mvalprop
  660. }
  661. for ks, raw := range mp {
  662. // Unmarshal map key. The core json library already decoded the key into a
  663. // string, so we handle that specially. Other types were quoted post-serialization.
  664. var k reflect.Value
  665. if targetType.Key().Kind() == reflect.String {
  666. k = reflect.ValueOf(ks)
  667. } else {
  668. k = reflect.New(targetType.Key()).Elem()
  669. if err := unmarshalValue(k, json.RawMessage(ks), keyprop); err != nil {
  670. return err
  671. }
  672. }
  673. // Unmarshal map value.
  674. v := reflect.New(targetType.Elem()).Elem()
  675. if err := unmarshalValue(v, raw, valprop); err != nil {
  676. return err
  677. }
  678. target.SetMapIndex(k, v)
  679. }
  680. return nil
  681. }
  682. // 64-bit integers can be encoded as strings. In this case we drop
  683. // the quotes and proceed as normal.
  684. isNum := targetType.Kind() == reflect.Int64 || targetType.Kind() == reflect.Uint64
  685. if isNum && strings.HasPrefix(string(inputValue), `"`) {
  686. inputValue = inputValue[1 : len(inputValue)-1]
  687. }
  688. // Use the encoding/json for parsing other value types.
  689. return json.Unmarshal(inputValue, target.Addr().Interface())
  690. }
  691. // jsonProperties returns parsed proto.Properties for the field and corrects JSONName attribute.
  692. func jsonProperties(f reflect.StructField, origName bool) *proto.Properties {
  693. var prop proto.Properties
  694. prop.Init(f.Type, f.Name, f.Tag.Get("protobuf"), &f)
  695. if origName || prop.JSONName == "" {
  696. prop.JSONName = prop.OrigName
  697. }
  698. return &prop
  699. }
  700. type fieldNames struct {
  701. orig, camel string
  702. }
  703. func acceptedJSONFieldNames(prop *proto.Properties) fieldNames {
  704. opts := fieldNames{orig: prop.OrigName, camel: prop.OrigName}
  705. if prop.JSONName != "" {
  706. opts.camel = prop.JSONName
  707. }
  708. return opts
  709. }
  710. // Writer wrapper inspired by https://blog.golang.org/errors-are-values
  711. type errWriter struct {
  712. writer io.Writer
  713. err error
  714. }
  715. func (w *errWriter) write(str string) {
  716. if w.err != nil {
  717. return
  718. }
  719. _, w.err = w.writer.Write([]byte(str))
  720. }
  721. // Map fields may have key types of non-float scalars, strings and enums.
  722. // The easiest way to sort them in some deterministic order is to use fmt.
  723. // If this turns out to be inefficient we can always consider other options,
  724. // such as doing a Schwartzian transform.
  725. type mapKeys []reflect.Value
  726. func (s mapKeys) Len() int { return len(s) }
  727. func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  728. func (s mapKeys) Less(i, j int) bool {
  729. return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface())
  730. }