Class: Code::Object::Json

Inherits:
Code::Object show all
Defined in:
lib/code/object/json.rb

Constant Summary collapse

CLASS_DOCUMENTATION =
{
  name: "Json",
  description: "parses json text and serializes values as json text.",
  examples: [
    "Json.parse(\"{}\")",
    "Json.generate({ a: 1 })",
    "Json.generate([1, 2], { pretty: true })"
  ]
}.freeze
CLASS_FUNCTIONS =
{
  "parse" => {
    name: "parse",
    description:
      "returns a value parsed from json text, or nothing when parsing fails.",
    examples: [
      "Json.parse(\"{}\")",
      "Json.parse(\"[1,2]\")",
      "Json.parse(\"bad json\")"
    ]
  },
  "generate" => {
    name: "generate",
    description:
      "returns json text for a value, optionally formatted for readability.",
    examples: [
      "Json.generate({ a: 1 })",
      "Json.generate([1, 2])",
      "Json.generate({ a: 1 }, { pretty: true })"
    ]
  }
}.freeze
MAX_NESTING =
100
MAX_ITEMS =
10_000

Constants inherited from Code::Object

INSTANCE_FUNCTIONS, NUMBER_CLASSES

Constants included from Concerns::Shared

Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS

Instance Attribute Summary

Attributes included from Concerns::Shared

#functions, #raw

Class Method Summary collapse

Methods inherited from Code::Object

class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, #initialize, instance_functions, maybe, #name, repeat, sorted_dictionary, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #call, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, #code_functions, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, #code_send, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

This class inherits a constructor from Code::Object

Class Method Details

.call(**args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/code/object/json.rb', line 47

def self.call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "parse"
    sig(args) { String }
    code_parse(code_value)
  when "generate"
    sig(args) { [Object, { pretty: Object::Boolean.maybe }] }

    if code_arguments.code_second.something?
      code_generate(
        code_value,
        pretty: code_arguments.code_second.code_get(:pretty)
      )
    else
      code_generate(code_value)
    end
  else
    super
  end
end

.code_generate(value, pretty: nil) ⇒ Object



83
84
85
# File 'lib/code/object/json.rb', line 83

def self.code_generate(value, pretty: nil)
  value.to_code.code_to_json(pretty: pretty)
end

.code_parse(value) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/code/object/json.rb', line 72

def self.code_parse(value)
  code_value = value.to_code
  ::Code.ensure_input_size!(code_value.raw, label: "json")

  parsed = ::JSON.parse(code_value.raw)
  validate_shape!(parsed)
  parsed.to_code
rescue JSON::ParserError
  Nothing.new
end

.function_documentation(scope) ⇒ Object



38
39
40
41
42
# File 'lib/code/object/json.rb', line 38

def self.function_documentation(scope)
  return CLASS_FUNCTIONS if scope == :class

  {}
end

.validate_shape!(value, depth: 0, count: 0) ⇒ Object

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/code/object/json.rb', line 87

def self.validate_shape!(value, depth: 0, count: 0)
  raise Error, "json is too deeply nested" if depth > MAX_NESTING
  raise Error, "json has too many items" if count > MAX_ITEMS

  case value
  when ::Array
    count += value.size
    value.each do |item|
      count = validate_shape!(item, depth: depth + 1, count: count)
    end
  when ::Hash
    count += value.size
    value.each_value do |item|
      count = validate_shape!(item, depth: depth + 1, count: count)
    end
  end

  count
end