equals.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jsonutils
  15. import (
  16. "yunion.io/x/pkg/sortedmap"
  17. )
  18. func (dict *JSONDict) Equals(json JSONObject) bool {
  19. dict2, ok := json.(*JSONDict)
  20. if !ok {
  21. return false
  22. }
  23. if len(dict.data) != len(dict2.data) {
  24. return false
  25. }
  26. aNoB, aB, bA, bNoA := sortedmap.Split(dict.data, dict2.data)
  27. if len(aNoB) > 0 || len(bNoA) > 0 {
  28. return false
  29. }
  30. for _, k := range aB.Keys() {
  31. aVal, _ := aB.Get(k)
  32. bVal, _ := bA.Get(k)
  33. aJson := aVal.(JSONObject)
  34. bJson := bVal.(JSONObject)
  35. if !aJson.Equals(bJson) {
  36. return false
  37. }
  38. }
  39. return true
  40. }
  41. func (arr *JSONArray) Equals(json JSONObject) bool {
  42. arr2, ok := json.(*JSONArray)
  43. if !ok {
  44. return false
  45. }
  46. if len(arr.data) != len(arr2.data) {
  47. return false
  48. }
  49. for i, v := range arr.data {
  50. if !v.Equals(arr2.data[i]) {
  51. return false
  52. }
  53. }
  54. return true
  55. }
  56. func (o *JSONString) Equals(json JSONObject) bool {
  57. o2, ok := json.(*JSONString)
  58. if !ok {
  59. return false
  60. }
  61. return o.data == o2.data
  62. }
  63. func (o *JSONInt) Equals(json JSONObject) bool {
  64. o2, ok := json.(*JSONInt)
  65. if !ok {
  66. return false
  67. }
  68. return o.data == o2.data
  69. }
  70. func (o *JSONFloat) Equals(json JSONObject) bool {
  71. o2, ok := json.(*JSONFloat)
  72. if !ok {
  73. return false
  74. }
  75. return o.data == o2.data
  76. }
  77. func (o *JSONBool) Equals(json JSONObject) bool {
  78. o2, ok := json.(*JSONBool)
  79. if !ok {
  80. return false
  81. }
  82. return o.data == o2.data
  83. }
  84. func (o *JSONValue) Equals(json JSONObject) bool {
  85. _, ok := json.(*JSONValue)
  86. if !ok {
  87. return false
  88. }
  89. return true
  90. }