string.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. "fmt"
  17. "strconv"
  18. "strings"
  19. )
  20. func (this *JSONString) String() string {
  21. return quoteString(this.data)
  22. }
  23. func (this *JSONValue) String() string {
  24. return "null"
  25. }
  26. func (this *JSONInt) String() string {
  27. return fmt.Sprintf("%d", this.data)
  28. }
  29. func (this *JSONFloat) String() string {
  30. if this.bit != 32 && this.bit != 64 {
  31. this.bit = 64
  32. }
  33. return strconv.FormatFloat(this.data, 'f', -1, this.bit)
  34. }
  35. func (this *JSONBool) String() string {
  36. if this.data {
  37. return "true"
  38. } else {
  39. return "false"
  40. }
  41. }
  42. func (this *JSONDict) String() string {
  43. sb := &strings.Builder{}
  44. this.buildString(sb)
  45. return sb.String()
  46. }
  47. func (this *JSONArray) String() string {
  48. sb := &strings.Builder{}
  49. this.buildString(sb)
  50. return sb.String()
  51. }