Class: Mingle::Json::JsonMingleCodec

Inherits:
BitGirderClass
  • Object
show all
Includes:
BitGirder::Core, Codec::MingleCodecImpl
Defined in:
lib/mingle/json.rb

Constant Summary collapse

KEY_TYPE =
"$type"
KEY_CONSTANT =
"$constant"
MSG_MISSING_TYPE_KEY =
%Q{Missing type key ("#{KEY_TYPE}")}

Constants included from BitGirder::Core

BitGirder::Core::ENV_BITGIRDER_DEBUG, BitGirder::Core::EXIT_FAILURE, BitGirder::Core::EXIT_SUCCESS

Instance Method Summary collapse

Instance Method Details

#as_buffer(obj) ⇒ Object



110
111
112
113
# File 'lib/mingle/json.rb', line 110

def as_buffer( obj )
    JSON.generate( from_mingle_value( obj ) )
#        JSON.generate( from_mingle_value( obj ) ).encode( "binary" )
end

#from_buffer(buf) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/mingle/json.rb', line 236

def from_buffer( buf )

    not_nil( buf, :buf )
    json = BitGirder::Io.parse_json( buf )

    if json.is_a?( Hash )

        codec_raise( MSG_MISSING_TYPE_KEY ) if json.empty?

        if ( res = as_mingle_value( json, nil ) ).is_a?( MingleStruct )
            res
        else
            codec_raise( "Expected struct" ) 
        end
    else
        codec_raise( "Unexpected top level JSON value" )
    end
end