decode.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package protojson
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "math"
  9. "strconv"
  10. "strings"
  11. "google.golang.org/protobuf/internal/encoding/json"
  12. "google.golang.org/protobuf/internal/encoding/messageset"
  13. "google.golang.org/protobuf/internal/errors"
  14. "google.golang.org/protobuf/internal/flags"
  15. "google.golang.org/protobuf/internal/pragma"
  16. "google.golang.org/protobuf/internal/set"
  17. "google.golang.org/protobuf/proto"
  18. pref "google.golang.org/protobuf/reflect/protoreflect"
  19. "google.golang.org/protobuf/reflect/protoregistry"
  20. )
  21. // Unmarshal reads the given []byte into the given proto.Message.
  22. func Unmarshal(b []byte, m proto.Message) error {
  23. return UnmarshalOptions{}.Unmarshal(b, m)
  24. }
  25. // UnmarshalOptions is a configurable JSON format parser.
  26. type UnmarshalOptions struct {
  27. pragma.NoUnkeyedLiterals
  28. // If AllowPartial is set, input for messages that will result in missing
  29. // required fields will not return an error.
  30. AllowPartial bool
  31. // If DiscardUnknown is set, unknown fields are ignored.
  32. DiscardUnknown bool
  33. // Resolver is used for looking up types when unmarshaling
  34. // google.protobuf.Any messages or extension fields.
  35. // If nil, this defaults to using protoregistry.GlobalTypes.
  36. Resolver interface {
  37. protoregistry.MessageTypeResolver
  38. protoregistry.ExtensionTypeResolver
  39. }
  40. decoder *json.Decoder
  41. }
  42. // Unmarshal reads the given []byte and populates the given proto.Message using
  43. // options in UnmarshalOptions object. It will clear the message first before
  44. // setting the fields. If it returns an error, the given message may be
  45. // partially set.
  46. func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
  47. // TODO: Determine if we would like to have an option for merging or only
  48. // have merging behavior. We should at least be consistent with textproto
  49. // marshaling.
  50. proto.Reset(m)
  51. if o.Resolver == nil {
  52. o.Resolver = protoregistry.GlobalTypes
  53. }
  54. o.decoder = json.NewDecoder(b)
  55. if err := o.unmarshalMessage(m.ProtoReflect(), false); err != nil {
  56. return err
  57. }
  58. // Check for EOF.
  59. val, err := o.decoder.Read()
  60. if err != nil {
  61. return err
  62. }
  63. if val.Type() != json.EOF {
  64. return unexpectedJSONError{val}
  65. }
  66. if o.AllowPartial {
  67. return nil
  68. }
  69. return proto.IsInitialized(m)
  70. }
  71. // unexpectedJSONError is an error that contains the unexpected json.Value. This
  72. // is returned by methods to provide callers the read json.Value that it did not
  73. // expect.
  74. // TODO: Consider moving this to internal/encoding/json for consistency with
  75. // errors that package returns.
  76. type unexpectedJSONError struct {
  77. value json.Value
  78. }
  79. func (e unexpectedJSONError) Error() string {
  80. return newError("unexpected value %s", e.value).Error()
  81. }
  82. // newError returns an error object. If one of the values passed in is of
  83. // json.Value type, it produces an error with position info.
  84. func newError(f string, x ...interface{}) error {
  85. var hasValue bool
  86. var line, column int
  87. for i := 0; i < len(x); i++ {
  88. if val, ok := x[i].(json.Value); ok {
  89. line, column = val.Position()
  90. hasValue = true
  91. break
  92. }
  93. }
  94. e := errors.New(f, x...)
  95. if hasValue {
  96. return errors.New("(line %d:%d): %v", line, column, e)
  97. }
  98. return e
  99. }
  100. // unmarshalMessage unmarshals a message into the given protoreflect.Message.
  101. func (o UnmarshalOptions) unmarshalMessage(m pref.Message, skipTypeURL bool) error {
  102. if isCustomType(m.Descriptor().FullName()) {
  103. return o.unmarshalCustomType(m)
  104. }
  105. jval, err := o.decoder.Read()
  106. if err != nil {
  107. return err
  108. }
  109. if jval.Type() != json.StartObject {
  110. return unexpectedJSONError{jval}
  111. }
  112. if err := o.unmarshalFields(m, skipTypeURL); err != nil {
  113. return err
  114. }
  115. return nil
  116. }
  117. // unmarshalFields unmarshals the fields into the given protoreflect.Message.
  118. func (o UnmarshalOptions) unmarshalFields(m pref.Message, skipTypeURL bool) error {
  119. messageDesc := m.Descriptor()
  120. if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
  121. return errors.New("no support for proto1 MessageSets")
  122. }
  123. var seenNums set.Ints
  124. var seenOneofs set.Ints
  125. fieldDescs := messageDesc.Fields()
  126. for {
  127. // Read field name.
  128. jval, err := o.decoder.Read()
  129. if err != nil {
  130. return err
  131. }
  132. switch jval.Type() {
  133. default:
  134. return unexpectedJSONError{jval}
  135. case json.EndObject:
  136. return nil
  137. case json.Name:
  138. // Continue below.
  139. }
  140. name, err := jval.Name()
  141. if err != nil {
  142. return err
  143. }
  144. // Unmarshaling a non-custom embedded message in Any will contain the
  145. // JSON field "@type" which should be skipped because it is not a field
  146. // of the embedded message, but simply an artifact of the Any format.
  147. if skipTypeURL && name == "@type" {
  148. o.decoder.Read()
  149. continue
  150. }
  151. // Get the FieldDescriptor.
  152. var fd pref.FieldDescriptor
  153. if strings.HasPrefix(name, "[") && strings.HasSuffix(name, "]") {
  154. // Only extension names are in [name] format.
  155. extName := pref.FullName(name[1 : len(name)-1])
  156. extType, err := o.findExtension(extName)
  157. if err != nil && err != protoregistry.NotFound {
  158. return errors.New("unable to resolve [%v]: %v", extName, err)
  159. }
  160. if extType != nil {
  161. fd = extType.TypeDescriptor()
  162. }
  163. } else {
  164. // The name can either be the JSON name or the proto field name.
  165. fd = fieldDescs.ByJSONName(name)
  166. if fd == nil {
  167. fd = fieldDescs.ByName(pref.Name(name))
  168. }
  169. if fd != nil && fd.IsWeak() && fd.Message().IsPlaceholder() {
  170. fd = nil // reset since the weak reference is not linked in
  171. }
  172. }
  173. if fd == nil {
  174. // Field is unknown.
  175. if o.DiscardUnknown {
  176. if err := skipJSONValue(o.decoder); err != nil {
  177. return err
  178. }
  179. continue
  180. }
  181. return newError("%v contains unknown field %s", messageDesc.FullName(), jval)
  182. }
  183. // Do not allow duplicate fields.
  184. num := uint64(fd.Number())
  185. if seenNums.Has(num) {
  186. return newError("%v contains repeated field %s", messageDesc.FullName(), jval)
  187. }
  188. seenNums.Set(num)
  189. // No need to set values for JSON null unless the field type is
  190. // google.protobuf.Value or google.protobuf.NullValue.
  191. if o.decoder.Peek() == json.Null && !isKnownValue(fd) && !isNullValue(fd) {
  192. o.decoder.Read()
  193. continue
  194. }
  195. switch {
  196. case fd.IsList():
  197. list := m.Mutable(fd).List()
  198. if err := o.unmarshalList(list, fd); err != nil {
  199. return errors.New("%v|%q: %v", fd.FullName(), name, err)
  200. }
  201. case fd.IsMap():
  202. mmap := m.Mutable(fd).Map()
  203. if err := o.unmarshalMap(mmap, fd); err != nil {
  204. return errors.New("%v|%q: %v", fd.FullName(), name, err)
  205. }
  206. default:
  207. // If field is a oneof, check if it has already been set.
  208. if od := fd.ContainingOneof(); od != nil {
  209. idx := uint64(od.Index())
  210. if seenOneofs.Has(idx) {
  211. return errors.New("%v: oneof is already set", od.FullName())
  212. }
  213. seenOneofs.Set(idx)
  214. }
  215. // Required or optional fields.
  216. if err := o.unmarshalSingular(m, fd); err != nil {
  217. return errors.New("%v|%q: %v", fd.FullName(), name, err)
  218. }
  219. }
  220. }
  221. }
  222. // findExtension returns protoreflect.ExtensionType from the resolver if found.
  223. func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
  224. xt, err := o.Resolver.FindExtensionByName(xtName)
  225. if err == nil {
  226. return xt, nil
  227. }
  228. return messageset.FindMessageSetExtension(o.Resolver, xtName)
  229. }
  230. func isKnownValue(fd pref.FieldDescriptor) bool {
  231. md := fd.Message()
  232. return md != nil && md.FullName() == "google.protobuf.Value"
  233. }
  234. func isNullValue(fd pref.FieldDescriptor) bool {
  235. ed := fd.Enum()
  236. return ed != nil && ed.FullName() == "google.protobuf.NullValue"
  237. }
  238. // unmarshalSingular unmarshals to the non-repeated field specified by the given
  239. // FieldDescriptor.
  240. func (o UnmarshalOptions) unmarshalSingular(m pref.Message, fd pref.FieldDescriptor) error {
  241. var val pref.Value
  242. var err error
  243. switch fd.Kind() {
  244. case pref.MessageKind, pref.GroupKind:
  245. val = m.NewField(fd)
  246. err = o.unmarshalMessage(val.Message(), false)
  247. default:
  248. val, err = o.unmarshalScalar(fd)
  249. }
  250. if err != nil {
  251. return err
  252. }
  253. m.Set(fd, val)
  254. return nil
  255. }
  256. // unmarshalScalar unmarshals to a scalar/enum protoreflect.Value specified by
  257. // the given FieldDescriptor.
  258. func (o UnmarshalOptions) unmarshalScalar(fd pref.FieldDescriptor) (pref.Value, error) {
  259. const b32 int = 32
  260. const b64 int = 64
  261. jval, err := o.decoder.Read()
  262. if err != nil {
  263. return pref.Value{}, err
  264. }
  265. kind := fd.Kind()
  266. switch kind {
  267. case pref.BoolKind:
  268. return unmarshalBool(jval)
  269. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  270. return unmarshalInt(jval, b32)
  271. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  272. return unmarshalInt(jval, b64)
  273. case pref.Uint32Kind, pref.Fixed32Kind:
  274. return unmarshalUint(jval, b32)
  275. case pref.Uint64Kind, pref.Fixed64Kind:
  276. return unmarshalUint(jval, b64)
  277. case pref.FloatKind:
  278. return unmarshalFloat(jval, b32)
  279. case pref.DoubleKind:
  280. return unmarshalFloat(jval, b64)
  281. case pref.StringKind:
  282. pval, err := unmarshalString(jval)
  283. if err != nil {
  284. return pval, err
  285. }
  286. return pval, nil
  287. case pref.BytesKind:
  288. return unmarshalBytes(jval)
  289. case pref.EnumKind:
  290. return unmarshalEnum(jval, fd)
  291. }
  292. panic(fmt.Sprintf("invalid scalar kind %v", kind))
  293. }
  294. func unmarshalBool(jval json.Value) (pref.Value, error) {
  295. if jval.Type() != json.Bool {
  296. return pref.Value{}, unexpectedJSONError{jval}
  297. }
  298. b, err := jval.Bool()
  299. return pref.ValueOf(b), err
  300. }
  301. func unmarshalInt(jval json.Value, bitSize int) (pref.Value, error) {
  302. switch jval.Type() {
  303. case json.Number:
  304. return getInt(jval, bitSize)
  305. case json.String:
  306. // Decode number from string.
  307. s := strings.TrimSpace(jval.String())
  308. if len(s) != len(jval.String()) {
  309. return pref.Value{}, errors.New("invalid number %v", jval.Raw())
  310. }
  311. dec := json.NewDecoder([]byte(s))
  312. jval, err := dec.Read()
  313. if err != nil {
  314. return pref.Value{}, err
  315. }
  316. return getInt(jval, bitSize)
  317. }
  318. return pref.Value{}, unexpectedJSONError{jval}
  319. }
  320. func getInt(jval json.Value, bitSize int) (pref.Value, error) {
  321. n, err := jval.Int(bitSize)
  322. if err != nil {
  323. return pref.Value{}, err
  324. }
  325. if bitSize == 32 {
  326. return pref.ValueOf(int32(n)), nil
  327. }
  328. return pref.ValueOf(n), nil
  329. }
  330. func unmarshalUint(jval json.Value, bitSize int) (pref.Value, error) {
  331. switch jval.Type() {
  332. case json.Number:
  333. return getUint(jval, bitSize)
  334. case json.String:
  335. // Decode number from string.
  336. s := strings.TrimSpace(jval.String())
  337. if len(s) != len(jval.String()) {
  338. return pref.Value{}, errors.New("invalid number %v", jval.Raw())
  339. }
  340. dec := json.NewDecoder([]byte(s))
  341. jval, err := dec.Read()
  342. if err != nil {
  343. return pref.Value{}, err
  344. }
  345. return getUint(jval, bitSize)
  346. }
  347. return pref.Value{}, unexpectedJSONError{jval}
  348. }
  349. func getUint(jval json.Value, bitSize int) (pref.Value, error) {
  350. n, err := jval.Uint(bitSize)
  351. if err != nil {
  352. return pref.Value{}, err
  353. }
  354. if bitSize == 32 {
  355. return pref.ValueOf(uint32(n)), nil
  356. }
  357. return pref.ValueOf(n), nil
  358. }
  359. func unmarshalFloat(jval json.Value, bitSize int) (pref.Value, error) {
  360. switch jval.Type() {
  361. case json.Number:
  362. return getFloat(jval, bitSize)
  363. case json.String:
  364. s := jval.String()
  365. switch s {
  366. case "NaN":
  367. if bitSize == 32 {
  368. return pref.ValueOf(float32(math.NaN())), nil
  369. }
  370. return pref.ValueOf(math.NaN()), nil
  371. case "Infinity":
  372. if bitSize == 32 {
  373. return pref.ValueOf(float32(math.Inf(+1))), nil
  374. }
  375. return pref.ValueOf(math.Inf(+1)), nil
  376. case "-Infinity":
  377. if bitSize == 32 {
  378. return pref.ValueOf(float32(math.Inf(-1))), nil
  379. }
  380. return pref.ValueOf(math.Inf(-1)), nil
  381. }
  382. // Decode number from string.
  383. if len(s) != len(strings.TrimSpace(s)) {
  384. return pref.Value{}, errors.New("invalid number %v", jval.Raw())
  385. }
  386. dec := json.NewDecoder([]byte(s))
  387. jval, err := dec.Read()
  388. if err != nil {
  389. return pref.Value{}, err
  390. }
  391. return getFloat(jval, bitSize)
  392. }
  393. return pref.Value{}, unexpectedJSONError{jval}
  394. }
  395. func getFloat(jval json.Value, bitSize int) (pref.Value, error) {
  396. n, err := jval.Float(bitSize)
  397. if err != nil {
  398. return pref.Value{}, err
  399. }
  400. if bitSize == 32 {
  401. return pref.ValueOf(float32(n)), nil
  402. }
  403. return pref.ValueOf(n), nil
  404. }
  405. func unmarshalString(jval json.Value) (pref.Value, error) {
  406. if jval.Type() != json.String {
  407. return pref.Value{}, unexpectedJSONError{jval}
  408. }
  409. return pref.ValueOf(jval.String()), nil
  410. }
  411. func unmarshalBytes(jval json.Value) (pref.Value, error) {
  412. if jval.Type() != json.String {
  413. return pref.Value{}, unexpectedJSONError{jval}
  414. }
  415. s := jval.String()
  416. enc := base64.StdEncoding
  417. if strings.ContainsAny(s, "-_") {
  418. enc = base64.URLEncoding
  419. }
  420. if len(s)%4 != 0 {
  421. enc = enc.WithPadding(base64.NoPadding)
  422. }
  423. b, err := enc.DecodeString(s)
  424. if err != nil {
  425. return pref.Value{}, err
  426. }
  427. return pref.ValueOf(b), nil
  428. }
  429. func unmarshalEnum(jval json.Value, fd pref.FieldDescriptor) (pref.Value, error) {
  430. switch jval.Type() {
  431. case json.String:
  432. // Lookup EnumNumber based on name.
  433. s := jval.String()
  434. if enumVal := fd.Enum().Values().ByName(pref.Name(s)); enumVal != nil {
  435. return pref.ValueOf(enumVal.Number()), nil
  436. }
  437. return pref.Value{}, newError("invalid enum value %q", jval)
  438. case json.Number:
  439. n, err := jval.Int(32)
  440. if err != nil {
  441. return pref.Value{}, err
  442. }
  443. return pref.ValueOf(pref.EnumNumber(n)), nil
  444. case json.Null:
  445. // This is only valid for google.protobuf.NullValue.
  446. if isNullValue(fd) {
  447. return pref.ValueOf(pref.EnumNumber(0)), nil
  448. }
  449. }
  450. return pref.Value{}, unexpectedJSONError{jval}
  451. }
  452. func (o UnmarshalOptions) unmarshalList(list pref.List, fd pref.FieldDescriptor) error {
  453. jval, err := o.decoder.Read()
  454. if err != nil {
  455. return err
  456. }
  457. if jval.Type() != json.StartArray {
  458. return unexpectedJSONError{jval}
  459. }
  460. switch fd.Kind() {
  461. case pref.MessageKind, pref.GroupKind:
  462. for {
  463. val := list.NewElement()
  464. err := o.unmarshalMessage(val.Message(), false)
  465. if err != nil {
  466. if e, ok := err.(unexpectedJSONError); ok {
  467. if e.value.Type() == json.EndArray {
  468. // Done with list.
  469. return nil
  470. }
  471. }
  472. return err
  473. }
  474. list.Append(val)
  475. }
  476. default:
  477. for {
  478. val, err := o.unmarshalScalar(fd)
  479. if err != nil {
  480. if e, ok := err.(unexpectedJSONError); ok {
  481. if e.value.Type() == json.EndArray {
  482. // Done with list.
  483. return nil
  484. }
  485. }
  486. return err
  487. }
  488. list.Append(val)
  489. }
  490. }
  491. return nil
  492. }
  493. func (o UnmarshalOptions) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) error {
  494. jval, err := o.decoder.Read()
  495. if err != nil {
  496. return err
  497. }
  498. if jval.Type() != json.StartObject {
  499. return unexpectedJSONError{jval}
  500. }
  501. // Determine ahead whether map entry is a scalar type or a message type in
  502. // order to call the appropriate unmarshalMapValue func inside the for loop
  503. // below.
  504. var unmarshalMapValue func() (pref.Value, error)
  505. switch fd.MapValue().Kind() {
  506. case pref.MessageKind, pref.GroupKind:
  507. unmarshalMapValue = func() (pref.Value, error) {
  508. val := mmap.NewValue()
  509. if err := o.unmarshalMessage(val.Message(), false); err != nil {
  510. return pref.Value{}, err
  511. }
  512. return val, nil
  513. }
  514. default:
  515. unmarshalMapValue = func() (pref.Value, error) {
  516. return o.unmarshalScalar(fd.MapValue())
  517. }
  518. }
  519. Loop:
  520. for {
  521. // Read field name.
  522. jval, err := o.decoder.Read()
  523. if err != nil {
  524. return err
  525. }
  526. switch jval.Type() {
  527. default:
  528. return unexpectedJSONError{jval}
  529. case json.EndObject:
  530. break Loop
  531. case json.Name:
  532. // Continue.
  533. }
  534. name, err := jval.Name()
  535. if err != nil {
  536. return err
  537. }
  538. // Unmarshal field name.
  539. pkey, err := unmarshalMapKey(name, fd.MapKey())
  540. if err != nil {
  541. return err
  542. }
  543. // Check for duplicate field name.
  544. if mmap.Has(pkey) {
  545. return newError("duplicate map key %q", jval)
  546. }
  547. // Read and unmarshal field value.
  548. pval, err := unmarshalMapValue()
  549. if err != nil {
  550. return err
  551. }
  552. mmap.Set(pkey, pval)
  553. }
  554. return nil
  555. }
  556. // unmarshalMapKey converts given string into a protoreflect.MapKey. A map key type is any
  557. // integral or string type.
  558. func unmarshalMapKey(name string, fd pref.FieldDescriptor) (pref.MapKey, error) {
  559. const b32 = 32
  560. const b64 = 64
  561. const base10 = 10
  562. kind := fd.Kind()
  563. switch kind {
  564. case pref.StringKind:
  565. return pref.ValueOf(name).MapKey(), nil
  566. case pref.BoolKind:
  567. switch name {
  568. case "true":
  569. return pref.ValueOf(true).MapKey(), nil
  570. case "false":
  571. return pref.ValueOf(false).MapKey(), nil
  572. }
  573. return pref.MapKey{}, errors.New("invalid value for boolean key %q", name)
  574. case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
  575. n, err := strconv.ParseInt(name, base10, b32)
  576. if err != nil {
  577. return pref.MapKey{}, err
  578. }
  579. return pref.ValueOf(int32(n)).MapKey(), nil
  580. case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
  581. n, err := strconv.ParseInt(name, base10, b64)
  582. if err != nil {
  583. return pref.MapKey{}, err
  584. }
  585. return pref.ValueOf(int64(n)).MapKey(), nil
  586. case pref.Uint32Kind, pref.Fixed32Kind:
  587. n, err := strconv.ParseUint(name, base10, b32)
  588. if err != nil {
  589. return pref.MapKey{}, err
  590. }
  591. return pref.ValueOf(uint32(n)).MapKey(), nil
  592. case pref.Uint64Kind, pref.Fixed64Kind:
  593. n, err := strconv.ParseUint(name, base10, b64)
  594. if err != nil {
  595. return pref.MapKey{}, err
  596. }
  597. return pref.ValueOf(uint64(n)).MapKey(), nil
  598. }
  599. panic(fmt.Sprintf("%s: invalid kind %s for map key", fd.FullName(), kind))
  600. }