class Serializer

class Serializer

lib/serializer.tya:12

Serializer provides the serialization/Serializer standard library API.

Source
# Serializer provides the serialization/Serializer standard library API.
class Serializer
  # Serializer.value stores instance state.
  # @type Nil
  value: nil

  # Serializer.initialize stores a value or serialized text.
  # @param value String value value.
  # @return Self the initialized object.
  initialize: value = nil ->
    self.value = value

  # Serializer.array_has? provides the serialization/Serializer standard library operation.
  # @param values Array values value.
  # @param value String value value.
  # @return Boolean whether the condition is true.
  array_has?: values, value ->
    i = 0
    while i < values.len()
      if values[i] == value
        return true
      i = i + 1
    false

  # Serializer.deserialize restores a value from serialization data.
  # @param data Array data value.
  # @param klass Any klass value.
  # @param options Dict options value.
  # @return Any the resulting value.
  deserialize: data = nil, klass = nil, options = nil ->
    self.from_data(data, klass, options)

  # Serializer.enter_seen provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param seen Any seen value.
  # @return Any the resulting value.
  enter_seen: value, seen ->
    id = serialization_id(value)
    i = 0
    while i < seen.len()
      if seen[i] == id
        raise error("serialization: cycle detected")
      i = i + 1
    seen.push(id)

  # Serializer.from_data provides the serialization/Serializer standard library operation.
  # @param data Array data value.
  # @param klass Any klass value.
  # @param options Dict options value.
  # @return Self the resulting value.
  from_data: data = nil, klass = nil, options = nil ->
    if data == nil
      data = self.value
    opts = Serializer(nil).options(options)
    if klass == nil
      return data
    if serialization_has_member(klass, "from_serialized")
      return klass.from_serialized(data)
    if serialization_kind(data) != "dict"
      raise error("serialization.from_data: class data must be a dict")
    obj = klass(nil, nil, nil, nil, nil, nil)
    defaults = opts.get("defaults", {})
    fields = opts.get("fields", nil)
    strict = opts.get("strict_fields", false)
    existing = serialization_public_fields(obj)
    keys = defaults.keys()
    i = 0
    while i < keys.len()
      key = keys[i]
      if fields == nil or Serializer(nil).array_has?(fields, key)
        obj[key] = Serializer(nil).from_data(defaults[key], nil, opts)
      i = i + 1
    keys = data.keys()
    i = 0
    while i < keys.len()
      key = keys[i]
      if key != opts["class_key"]
        if fields == nil or Serializer(nil).array_has?(fields, key)
          if strict and not existing.has?(key) and not defaults.has?(key)
            raise error("serialization.from_data: unknown field " + key)
          obj[key] = Serializer(nil).from_data(data[key], nil, opts)
      i = i + 1
    obj

  # Serializer.from_json provides the serialization/Serializer standard library operation.
  # @param text String text value.
  # @param klass Any klass value.
  # @param options Dict options value.
  # @return Self the resulting value.
  from_json: text = nil, klass = nil, options = nil ->
    if text == nil
      text = self.value
    Serializer(nil).from_data(json.Json(text).parse(), klass, options)

  # Serializer.from_toml provides the serialization/Serializer standard library operation.
  # @param text String text value.
  # @param klass Any klass value.
  # @param options Dict options value.
  # @return Self the resulting value.
  from_toml: text = nil, klass = nil, options = nil ->
    if text == nil
      text = self.value
    Serializer(nil).from_data(toml.Toml(text).parse(), klass, options)

  # Serializer.options provides the serialization/Serializer standard library operation.
  # @param options Dict options value.
  # @return Dict the resulting value.
  options: options ->
    opts =
      bytes: nil
      include_class: false
      class_key: "$class"
      fields: nil
      defaults: {}
      strict_fields: false
    if options == nil
      return opts
    allowed = opts.keys()
    keys = options.keys()
    i = 0
    while i < keys.len()
      key = keys[i]
      if not Serializer(nil).array_has?(allowed, key)
        raise error("serialization.options: unknown option " + key)
      opts[key] = options[key]
      i = i + 1
    if (
      opts["bytes"] != nil and opts["bytes"] != "base64" and opts["bytes"] != "array"
    )
      raise error("serialization.options: bytes must be base64 or array")
    opts

  # Serializer.serialize converts a value into serialization data.
  # @param value String value value.
  # @param options Dict options value.
  # @return Any the resulting value.
  serialize: value = nil, options = nil ->
    self.to_data(value, options)

  # Serializer.to_data provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param options Dict options value.
  # @return Any the resulting value.
  to_data: value = nil, options = nil ->
    if value == nil
      value = self.value
    opts = Serializer(nil).options(options)
    Serializer(nil).to_data_seen(value, opts, [])

  # Serializer.to_data_seen provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param opts Any opts value.
  # @param seen Any seen value.
  # @return Any the resulting value.
  to_data_seen: value, opts, seen ->
    kind = serialization_kind(value)
    if kind == "nil" or kind == "bool" or kind == "number" or kind == "string"
      return value
    if kind == "bytes"
      mode = opts.get("bytes", nil)
      if mode == "base64"
        return base64.Base64.encode(value)
      if mode == "array"
        return bytes_array(value)
      raise error("serialization.to_data: bytes require bytes option")
    if kind == "array" or kind == "dict" or kind == "object"
      Serializer(nil).enter_seen(value, seen)
      if kind == "array"
        out = []
        i = 0
        while i < value.len()
          out.push(Serializer(nil).to_data_seen(value[i], opts, seen))
          i = i + 1
        seen.pop()
        return out
      data = value
      if kind == "object"
        if serialization_has_member(value, "to_data")
          data = value.to_data()
          if serialization_kind(data) == "object"
            raise error("serialization.to_data: to_data returned unsupported object")
          seen.pop()
          return Serializer(nil).to_data_seen(data, opts, seen)
        if serialization_has_member(value, "to_serialized")
          data = value.to_serialized()
          seen.pop()
          return Serializer(nil).to_data_seen(data, opts, seen)
        data = serialization_public_fields(value)
      out = {}
      if kind == "object" and opts["include_class"]
        out[opts["class_key"]] = value.class.name
      keys = data.keys()
      fields = opts.get("fields", nil)
      i = 0
      while i < keys.len()
        key = keys[i]
        if key.class != String
          raise error("serialization.to_data: dictionary keys must be strings")
        if fields == nil or Serializer(nil).array_has?(fields, key)
          out[key] = Serializer(nil).to_data_seen(data[key], opts, seen)
        i = i + 1
      seen.pop()
      return out
    raise error("serialization.to_data: unsupported " + kind)

  # Serializer.to_json provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param options Dict options value.
  # @return Any the resulting value.
  to_json: value = nil, options = nil ->
    if value == nil
      value = self.value
    json.Json(Serializer(nil).to_data(value, options)).stringify()

  # Serializer.to_toml provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param options Dict options value.
  # @return Any the resulting value.
  to_toml: value = nil, options = nil ->
    if value == nil
      value = self.value
    data = Serializer(nil).to_data(value, options)
    if serialization_kind(data) != "dict"
      raise error("serialization.to_toml: top-level value must serialize to a dict")
    toml.Toml(data).stringify()

Instance Variables

value

Serializer.value

lib/serializer.tya:15

Serializer.value stores instance state.

Source
  # Serializer.value stores instance state.
  # @type Nil
  value: nil

Methods

array_has?

Serializer.array_has?(values, value)

lib/serializer.tya:27

Serializer.array_has? provides the serialization/Serializer standard library operation.

Source
  # Serializer.array_has? provides the serialization/Serializer standard library operation.
  # @param values Array values value.
  # @param value String value value.
  # @return Boolean whether the condition is true.
  array_has?: values, value ->
    i = 0
    while i < values.len()
      if values[i] == value
        return true
      i = i + 1
    false

deserialize

Serializer.deserialize(data = nil, klass = nil, options = nil)

lib/serializer.tya:40

Serializer.deserialize restores a value from serialization data.

Source
  # Serializer.deserialize restores a value from serialization data.
  # @param data Array data value.
  # @param klass Any klass value.
  # @param options Dict options value.
  # @return Any the resulting value.
  deserialize: data = nil, klass = nil, options = nil ->
    self.from_data(data, klass, options)

enter_seen

Serializer.enter_seen(value, seen)

lib/serializer.tya:47

Serializer.enter_seen provides the serialization/Serializer standard library operation.

Source
  # Serializer.enter_seen provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param seen Any seen value.
  # @return Any the resulting value.
  enter_seen: value, seen ->
    id = serialization_id(value)
    i = 0
    while i < seen.len()
      if seen[i] == id
        raise error("serialization: cycle detected")
      i = i + 1
    seen.push(id)

from_data

Serializer.from_data(data = nil, klass = nil, options = nil)

lib/serializer.tya:61

Serializer.from_data provides the serialization/Serializer standard library operation.

Source
  # Serializer.from_data provides the serialization/Serializer standard library operation.
  # @param data Array data value.
  # @param klass Any klass value.
  # @param options Dict options value.
  # @return Self the resulting value.
  from_data: data = nil, klass = nil, options = nil ->
    if data == nil
      data = self.value
    opts = Serializer(nil).options(options)
    if klass == nil
      return data
    if serialization_has_member(klass, "from_serialized")
      return klass.from_serialized(data)
    if serialization_kind(data) != "dict"
      raise error("serialization.from_data: class data must be a dict")
    obj = klass(nil, nil, nil, nil, nil, nil)
    defaults = opts.get("defaults", {})
    fields = opts.get("fields", nil)
    strict = opts.get("strict_fields", false)
    existing = serialization_public_fields(obj)
    keys = defaults.keys()
    i = 0
    while i < keys.len()
      key = keys[i]
      if fields == nil or Serializer(nil).array_has?(fields, key)
        obj[key] = Serializer(nil).from_data(defaults[key], nil, opts)
      i = i + 1
    keys = data.keys()
    i = 0
    while i < keys.len()
      key = keys[i]
      if key != opts["class_key"]
        if fields == nil or Serializer(nil).array_has?(fields, key)
          if strict and not existing.has?(key) and not defaults.has?(key)
            raise error("serialization.from_data: unknown field " + key)
          obj[key] = Serializer(nil).from_data(data[key], nil, opts)
      i = i + 1
    obj

from_json

Serializer.from_json(text = nil, klass = nil, options = nil)

lib/serializer.tya:100

Serializer.from_json provides the serialization/Serializer standard library operation.

Source
  # Serializer.from_json provides the serialization/Serializer standard library operation.
  # @param text String text value.
  # @param klass Any klass value.
  # @param options Dict options value.
  # @return Self the resulting value.
  from_json: text = nil, klass = nil, options = nil ->
    if text == nil
      text = self.value
    Serializer(nil).from_data(json.Json(text).parse(), klass, options)

from_toml

Serializer.from_toml(text = nil, klass = nil, options = nil)

lib/serializer.tya:110

Serializer.from_toml provides the serialization/Serializer standard library operation.

Source
  # Serializer.from_toml provides the serialization/Serializer standard library operation.
  # @param text String text value.
  # @param klass Any klass value.
  # @param options Dict options value.
  # @return Self the resulting value.
  from_toml: text = nil, klass = nil, options = nil ->
    if text == nil
      text = self.value
    Serializer(nil).from_data(toml.Toml(text).parse(), klass, options)

initialize

Serializer.initialize(value = nil)

lib/serializer.tya:20

Serializer.initialize stores a value or serialized text.

Source
  # Serializer.initialize stores a value or serialized text.
  # @param value String value value.
  # @return Self the initialized object.
  initialize: value = nil ->
    self.value = value

options

Serializer.options(options)

lib/serializer.tya:118

Serializer.options provides the serialization/Serializer standard library operation.

Source
  # Serializer.options provides the serialization/Serializer standard library operation.
  # @param options Dict options value.
  # @return Dict the resulting value.
  options: options ->
    opts =
      bytes: nil
      include_class: false
      class_key: "$class"
      fields: nil
      defaults: {}
      strict_fields: false
    if options == nil
      return opts
    allowed = opts.keys()
    keys = options.keys()
    i = 0
    while i < keys.len()
      key = keys[i]
      if not Serializer(nil).array_has?(allowed, key)
        raise error("serialization.options: unknown option " + key)
      opts[key] = options[key]
      i = i + 1
    if (
      opts["bytes"] != nil and opts["bytes"] != "base64" and opts["bytes"] != "array"
    )
      raise error("serialization.options: bytes must be base64 or array")
    opts

serialize

Serializer.serialize(value = nil, options = nil)

lib/serializer.tya:147

Serializer.serialize converts a value into serialization data.

Source
  # Serializer.serialize converts a value into serialization data.
  # @param value String value value.
  # @param options Dict options value.
  # @return Any the resulting value.
  serialize: value = nil, options = nil ->
    self.to_data(value, options)

to_data

Serializer.to_data(value = nil, options = nil)

lib/serializer.tya:154

Serializer.to_data provides the serialization/Serializer standard library operation.

Source
  # Serializer.to_data provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param options Dict options value.
  # @return Any the resulting value.
  to_data: value = nil, options = nil ->
    if value == nil
      value = self.value
    opts = Serializer(nil).options(options)
    Serializer(nil).to_data_seen(value, opts, [])

to_data_seen

Serializer.to_data_seen(value, opts, seen)

lib/serializer.tya:165

Serializer.to_data_seen provides the serialization/Serializer standard library operation.

Source
  # Serializer.to_data_seen provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param opts Any opts value.
  # @param seen Any seen value.
  # @return Any the resulting value.
  to_data_seen: value, opts, seen ->
    kind = serialization_kind(value)
    if kind == "nil" or kind == "bool" or kind == "number" or kind == "string"
      return value
    if kind == "bytes"
      mode = opts.get("bytes", nil)
      if mode == "base64"
        return base64.Base64.encode(value)
      if mode == "array"
        return bytes_array(value)
      raise error("serialization.to_data: bytes require bytes option")
    if kind == "array" or kind == "dict" or kind == "object"
      Serializer(nil).enter_seen(value, seen)
      if kind == "array"
        out = []
        i = 0
        while i < value.len()
          out.push(Serializer(nil).to_data_seen(value[i], opts, seen))
          i = i + 1
        seen.pop()
        return out
      data = value
      if kind == "object"
        if serialization_has_member(value, "to_data")
          data = value.to_data()
          if serialization_kind(data) == "object"
            raise error("serialization.to_data: to_data returned unsupported object")
          seen.pop()
          return Serializer(nil).to_data_seen(data, opts, seen)
        if serialization_has_member(value, "to_serialized")
          data = value.to_serialized()
          seen.pop()
          return Serializer(nil).to_data_seen(data, opts, seen)
        data = serialization_public_fields(value)
      out = {}
      if kind == "object" and opts["include_class"]
        out[opts["class_key"]] = value.class.name
      keys = data.keys()
      fields = opts.get("fields", nil)
      i = 0
      while i < keys.len()
        key = keys[i]
        if key.class != String
          raise error("serialization.to_data: dictionary keys must be strings")
        if fields == nil or Serializer(nil).array_has?(fields, key)
          out[key] = Serializer(nil).to_data_seen(data[key], opts, seen)
        i = i + 1
      seen.pop()
      return out
    raise error("serialization.to_data: unsupported " + kind)

to_json

Serializer.to_json(value = nil, options = nil)

lib/serializer.tya:220

Serializer.to_json provides the serialization/Serializer standard library operation.

Source
  # Serializer.to_json provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param options Dict options value.
  # @return Any the resulting value.
  to_json: value = nil, options = nil ->
    if value == nil
      value = self.value
    json.Json(Serializer(nil).to_data(value, options)).stringify()

to_toml

Serializer.to_toml(value = nil, options = nil)

lib/serializer.tya:229

Serializer.to_toml provides the serialization/Serializer standard library operation.

Source
  # Serializer.to_toml provides the serialization/Serializer standard library operation.
  # @param value String value value.
  # @param options Dict options value.
  # @return Any the resulting value.
  to_toml: value = nil, options = nil ->
    if value == nil
      value = self.value
    data = Serializer(nil).to_data(value, options)
    if serialization_kind(data) != "dict"
      raise error("serialization.to_toml: top-level value must serialize to a dict")
    toml.Toml(data).stringify()