Lines Matching +full:- +full:j

10 %%   http://www.apache.org/licenses/LICENSE-2.0
23 -module(thrift_json_protocol).
25 -behaviour(thrift_protocol).
27 -include("thrift_constants.hrl").
28 -include("thrift_protocol.hrl").
30 -export([new/1, new/2,
38 -record(json_context, {
45 -record(json_protocol, {
50 -type state() :: #json_protocol{}.
51 -include("thrift_protocol_behaviour.hrl").
53 -define(VERSION_1, 1).
54 -define(JSON_DOUBLE_PRECISION, 16).
56 typeid_to_json(?tType_BOOL) -> "tf";
57 typeid_to_json(?tType_BYTE) -> "i8";
58 typeid_to_json(?tType_DOUBLE) -> "dbl";
59 typeid_to_json(?tType_I8) -> "i8";
60 typeid_to_json(?tType_I16) -> "i16";
61 typeid_to_json(?tType_I32) -> "i32";
62 typeid_to_json(?tType_I64) -> "i64";
63 typeid_to_json(?tType_STRING) -> "str";
64 typeid_to_json(?tType_STRUCT) -> "rec";
65 typeid_to_json(?tType_MAP) -> "map";
66 typeid_to_json(?tType_SET) -> "set";
67 typeid_to_json(?tType_LIST) -> "lst".
69 json_to_typeid("tf") -> ?tType_BOOL;
70 json_to_typeid("dbl") -> ?tType_DOUBLE;
71 json_to_typeid("i8") -> ?tType_I8;
72 json_to_typeid("i16") -> ?tType_I16;
73 json_to_typeid("i32") -> ?tType_I32;
74 json_to_typeid("i64") -> ?tType_I64;
75 json_to_typeid("str") -> ?tType_STRING;
76 json_to_typeid("rec") -> ?tType_STRUCT;
77 json_to_typeid("map") -> ?tType_MAP;
78 json_to_typeid("set") -> ?tType_SET;
79 json_to_typeid("lst") -> ?tType_LIST.
81 start_context(object) -> "{";
82 start_context(array) -> "[".
84 end_context(object) -> "}";
85 end_context(array) -> "]".
88 new(Transport) ->
91 new(Transport, _Options) ->
95 flush_transport(This = #json_protocol{transport = Transport}) ->
102 close_transport(This = #json_protocol{transport = Transport}) ->
114 write(#json_protocol{context_stack = Stack} = State0, {enter_context, Type}) ->
121 write(#json_protocol{context_stack = [CurrCtxt|Stack]} = State0, {exit_context}) ->
131 {context_pre_item, _}) -> {This0, ok};
133 {context_pre_item, MayNeedQuotes}) ->
138 {array, N, _, _} when N > 0 -> % array element (not first)
140 {object, 0, _, true} -> % non-string object key (first)
142 {object, N, 0, true} when N > 0 -> % non-string object key (not first)
144 {object, N, 0, false} when N > 0-> % string object key (not first)
146 _ -> % no pre-field necessary
152 {context_post_item, _}) -> {This0, ok};
154 {context_post_item, MayNeedQuotes}) ->
159 {object, 0, true} -> % non-string object key
161 {object, 0, false} -> % string object key
163 _ -> % no pre-field necessary
172 seqid = Seqid}) ->
181 write(This, message_end) ->
188 id = Id}) ->
197 write(This, field_stop) ->
200 write(This, field_end) ->
207 size = Size}) ->
216 write(This, map_end) ->
224 size = Size}) ->
231 write(This, list_end) ->
239 size = Size}) ->
246 write(This, set_end) ->
251 write(This, #protocol_struct_begin{}) ->
256 write(This, struct_end) ->
261 write(This, {bool, true}) -> write_values(This, [
267 write(This, {bool, false}) -> write_values(This, [
273 write(This, {byte, Byte}) -> write_values(This, [
279 write(This, {i16, I16}) ->
282 write(This, {i32, I32}) ->
285 write(This, {i64, I64}) ->
288 write(This, {double, Double}) -> write_values(This, [
294 write(This0, {string, Str}) -> write_values(This0, [
297 true -> Str;
298 false -> <<"\"", (list_to_binary(Str))/binary, "\"">>
306 write(This = #json_protocol{transport = Trans}, Data) ->
311 write_values(This0, ValueList) ->
313 fun(Val, ThisIn) ->
327 read_all(#json_protocol{transport = Transport0} = State) ->
336 read_all_1(Transport0, IoList) ->
339 {ok, <<>>} -> % nothing read: assume we're done
341 {ok, Data} -> % character successfully read; read more
343 {error, 'EOF'} -> % we're done
351 expect(#json_protocol{jsx={event, {Type, Data}=Ev, [Next|Rest]}}=State, ExpectedType) ->
354 true ->
356 false ->
360 expect(#json_protocol{jsx={event, Event, Next}}=State, ExpectedEvent) ->
363 convert_data(integer, I) -> list_to_integer(I);
364 convert_data(float, F) -> list_to_float(F);
365 convert_data(_, D) -> D.
367 expect_many(State, ExpectedList) ->
370 expect_many_1(State, [], ResultList, Status) ->
372 expect_many_1(State, [Expected|ExpTail], ResultList, _PrevStatus) ->
377 error -> expect_many_1(State1, [], NewResultList, Status);
378 ok -> expect_many_1(State1, ExpTail, NewResultList, Status)
382 expect_nodata(This, ExpectedList) ->
384 {State, {ok, _}} ->
386 Error ->
390 read_field(#json_protocol{jsx={event, Field, [Next|Rest]}} = State) ->
394 read(This0, message_begin) ->
399 {This2, {ok, [_, Version, Name, Type, SeqId]}} ->
401 true ->
405 false ->
408 Other -> Other
411 read(This, message_end) ->
414 read(This, struct_begin) ->
417 read(This, struct_end) ->
420 read(This0, field_begin) ->
429 {ok, [FieldIdStr, _, FieldType]} ->
433 {error,[{unexpected_json_event, {end_object,none}}]} ->
435 Other ->
440 read(This, field_end) ->
444 read(This0, map_begin) ->
455 {This1, {ok, [_, Ktype, Vtype, Size, _]}} ->
459 Other -> Other
462 read(This, map_end) ->
465 read(This0, list_begin) ->
472 {This1, {ok, [_, Etype, Size]}} ->
476 Other -> Other
479 read(This, list_end) ->
483 read(This0, set_begin) ->
490 {This1, {ok, [_, Etype, Size]}} ->
494 Other -> Other
497 read(This, set_end) ->
500 read(This0, field_stop) ->
504 read(This0, bool) ->
507 {literal, I} ->
509 _Other ->
514 read(This0, byte) ->
517 {key, K} ->
519 {integer, I} ->
521 _Other ->
526 read(This0, i16) ->
529 read(This0, i32) ->
532 read(This0, i64) ->
535 read(This0, double) ->
538 {float, I} ->
540 _Other ->
546 read(This0, string) ->
549 {string, I} ->
551 {key, J} ->
552 {ok, J};
553 _Other ->
560 %% returns a (fun() -> thrift_protocol())
561 new_protocol_factory(TransportFactory, _Options) ->
563 F = fun() ->