Class: JSOG

Inherits:
Object
  • Object
show all
Defined in:
lib/jsog.rb

Class Method Summary collapse

Class Method Details

.decode(obj) ⇒ Hash

Convert a (Hash) object graph that was encoded with encode() back to a normal structure with cyclic references.

Parameters:

  • obj (Hash)

    an object graph that was created with encode()

Returns:

  • (Hash)

    an object graph with direct references instead of @ref



21
22
23
# File 'lib/jsog.rb', line 21

def decode(obj)
  return do_decode(obj, {})
end

.dump(obj) ⇒ Object

Just like JSON.dump(), but outputs JSOG.



31
32
33
# File 'lib/jsog.rb', line 31

def dump(obj)
  return JSON.dump(encode(obj))
end

.encode(obj) ⇒ Hash

Convert a (Hash) object graph with potential cyclic references into a simpler structure which contains @id and @ref references.

Parameters:

  • obj (Hash)

    the object graph to convert

Returns:

  • (Hash)

    an object graph with @ref instead of duplicate references



12
13
14
# File 'lib/jsog.rb', line 12

def encode(obj)
  return do_encode(obj, {})
end

.parse(str) ⇒ Object

Just like JSON.parse(), but reads JSOG. JSON is safe too.



26
27
28
# File 'lib/jsog.rb', line 26

def parse(str)
  return decode(JSON.parse(str))
end