Marcel, Sorry for the confusion, I missed one significant detail. mochijson2:encode/1 will return an iolist (a list of lists and binaries). Wrap the call to mochijson2:encode/1 in erlang:iolist_to_binary/1, like so:
Value = erlang:iolist_to_binary(mochijson2:encode({struct, [{id, 22}]})). Hope that helps. On Mon, Jan 9, 2012 at 7:56 AM, Marcel Müller <marcel.muel...@mlvv.de>wrote: > Hi Sean, > > thanks for your help but we found another problem: > > after we insert the valid json: > {ok,Pid} = riakc_pb_socket:start_link("10.10.10.74", 8087), > Value = mochijson2:encode({struct, [{id, 22}]}), > In = riakc_obj:new(<<"mm">>,<<"1">>,Value,"application/json"), > riakc_pb_socket:put(Pid,In) > > we read it from erlang: > > {ok, Obj} = riakc_pb_socket:get(Pid,"mm","1"), > io:format("Obj: ~p~n",[Obj]), > ContentType = riakc_obj:get_content_type(Obj), > io:format("ContentType: ~p~n",[ContentType]), > > Obj: {riakc_obj,"mm","1", > > <<107,206,97,96,96,96,204,96,202,5,82,28,202,156,255,126,250, > > 115,109,75,206,96,74,228,206,99,101,152,17,87,127,130,47,11,0>>, > [{{dict,3,16,16,8,80,48, > {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]}, > {{[],[],[],[],[],[],[],[],[],[], > > [[<<"X-Riak-VTag">>,51,122,101,57,117,75,111,98,113, > 118,97,77,100,117,114,76,97,56,122,68,115,70], > > [<<"content-type">>,97,112,112,108,105,99,97,116, > > 105,111,110,47,120,45,101,114,108,97,110,103,45, > 98,105,110,97,114,121]], > [],[], > > [[<<"X-Riak-Last-Modified">>|{1326,113432,240315}]], > [],[]}}}, > <<131,107,0,4,84,69,83,84>>}], > undefined,undefined} > ContentType: "application/x-erlang-binary" > > We received every time x-erlang-binary > > When we wrote the same over the http api for example: > > curl -X PUT -H "Content-Type: application/json" -i > http://127.0.0.1:8098/riak/mm/2 -d '{"email:","email to email.com > ","pass":"123123"}' > > Obj: {riakc_obj,"mm","2", > > <<107,206,97,96,96,96,204,96,202,5,82,28,202,156,255,126,250, > > 115,157,14,206,96,74,100,204,99,101,232,115,175,63,193,151,5, > 0>>, > [{{dict,3,16,16,8,80,48, > {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]}, > {{[],[],[],[],[],[],[],[],[],[], > > [[<<"X-Riak-VTag">>,122,97,52,89,80,115,106,112,121, > 57,118,98,68,81,114,66,56,72,109,78,52], > > [<<"content-type">>,97,112,112,108,105,99,97,116, > 105,111,110,47,106,115,111,110]], > [],[], > > [[<<"X-Riak-Last-Modified">>|{1326,107534,668061}]], > [],[]}}}, > <<"{\"email:\",\"email to email.com > \",\"pass\":\"123123\"}">>}], > undefined,undefined} > ContentType: "application/json" > > > It looks like the risk_obj:new don't write the right content-type. > > Can you imagine why ? > > Thanks > Marcel > > > Am 07.01.2012 um 18:29 schrieb Sean Cribbs: > > Marcel, > > {ok,Pid} = riakc_pb_socket:start_link("127.0.0.1", 8087), >> Tuple = "{\"id\",22}", >> > > That is not valid JSON, first of all. > > >> Obj = riakc_obj:new("mm","1",Tuple), >> > > Make sure that you set the content type here, otherwise it won't be > detected on the Java side. (example below) > > >> riakc_pb_socket:put(Pid,Obj), >> >> After then we try to read from risk-java-client: >> >> RiakClient httpClient = new RiakClient("http://10.10.10.61:8098/riak"); >> IRiakClient client = RiakFactory.httpClient(httpClient); >> Bucket b = client.fetchBucket("mm").execute(); >> IRiakObject fetched = b.fetch("1").execute(); >> String decodedString = new String(fetched.getValue()); >> System.out.println(decodedString); >> >> I convert the Object to an HexString: 83 6B 00 05 7B 22 6E 22 7D >> >> > Decoding that byte array in Erlang gives: > > 1> <<16#83,16#6B,0,5,16#7B,16#22,16#6E,16#22,16#7D>>. > <<131,107,0,5,123,34,110,34,125>> > 2> binary_to_term(v(1)). > "{\"n\"}" > 3> > > The default behavior of the Erlang client is to serialize terms to binary > unless the content type is explicit and the value of the object is a > binary. Try this instead: > > Value = mochijson2:encode({struct, [{id, 22}]}), > Obj = riakc_obj:new(<<"mm">>,<<"1">>,Value,"application/json"), > riakc_pb_socket:put(Pid, Obj) > > Then re-read it from the Java side and you should get what you expect. > > My question is: >> >> 1. What are the first 4 bytes ? >> > > A binary that starts with byte 131 is typically in Erlang > term_to_binary/binary_to_term format. > > >> 2. how can i receive a string with out the 4 bytes ? >> > > See above. > > >> 3. How can i convert the string into a JSON Object ? >> >> > See above, and then use Jackson to decode the value (I believe the Java > client may do this automatically for you). > > -- > Sean Cribbs <s...@basho.com> > Software Engineer > Basho Technologies, Inc. > http://basho.com/ > > > -- Sean Cribbs <s...@basho.com> Software Engineer Basho Technologies, Inc. http://basho.com/
_______________________________________________ riak-users mailing list riak-users@lists.basho.com http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com