hfɍdZdZddlZddlmZddlZddlZddlmZddl Z ddl Z ddl m Z ddl mZddl mZdd l mZd ZeejjejjejjejjgZeejjejjgZeejjejjgZd Zd Zd Ze j dZ!e j dZ"Gdde#Z$Gdde$Z%Gdde$Z& d5dZ' d6dZ(dZ)Gdde*Z+dZ,dZ-d Z. d7d"Z/ d7d#Z0e1e2fZ3Gd$d%e*Z4d8d&Z5d'Z6d(Z7d)Z8d*d+gd,d-gd,d-gd.d/gd0d1gd,d-gd2d3gd4Z9dS)9atContains routines for printing protocol messages in JSON format. Simple usage example: # Create a proto object and serialize it to a json format string. message = my_proto_pb2.MyMessage(foo='bar') json_string = json_format.MessageToJson(message) # Parse a json format string to proto object. message = json_format.Parse(json_string, my_proto_pb2.MyMessage()) zjieluo@google.com (Jie Luo)N) OrderedDict) methodcaller) type_checkers) descriptor)message_factory)symbol_databasez%Y-%m-%dT%H:%M:%SInfinityz -InfinityNaNu.[-](?![-])|(?jr j}n j}||vrYt ri||<n j tj jkrg||< j||<n;#t0$r.} t3d j| | d} ~ wwxYw|S)z?Converts normal message according to Proto3 JSON Specification.valuetruefalsec<g|]}|Sr)_FieldToJsonObject).0kr5r;s r z8_Printer._RegularMessageToJsonObject..s9&&&--eQ77&&&rz[%s]z#Failed to serialize {0} field: {1}.N) ListFieldsr!name json_namer6r1fields_by_name isinstanceboolstrrNlabelrr/LABEL_REPEATED is_extensionrCr rBfieldscpp_typeCPPTYPE_MESSAGEcontaining_oneof default_value ValueErrorrr9) r;rr@r\rJrSv_fieldjs_mapkey recorded_keyrHer5s ` @rrGz$_Printer._RegularMessageToJsonObjects    ! !F8N ;;,%  + !$$$ u   ;&5g>'& % %c#t$$ &'% &  XXl#'#:#:s$%$%F< "T(( [J6E E E&&&&&$&&&"T((   ;%/)$,,UE::"T((,,UE::"T((  ,K$/'. K KE{j8GGG~!;!KKK$L   - #:DD?D RZZ     KBtHH{j8GGGBtHH..ue6IJJBtHH NNN / 6 6uz1 E E G GLMNN IsG G%% H/)HHc|jtjjkr||S|jtjjkra|jr|S|jjdkrdS|jj |d}||j S|jj rtd|S|jtjjkrC|jtjjkr't#j|dS|S|jtjjkrt+|S|jt,vrt/|S|jt0vrt3j|r|dkrt6St8St3j|rt<S|jtjjkr=|j r"tCtE||j StGj$|S|S)z!,...244bk i  + +K < <Converts Value message according to Proto3 JSON Specification.kindN null_value list_value number_valuezTFail to serialize Infinity for Value.number_value, which would parse as string_valuezOFail to serialize NaN for Value.number_value, which would parse as string_value) WhichOneof_ListValueMessageToJsonObjectrrrwrxrar{getattrrBrUrN)r;rwhichrJoneof_descriptors r_ValueMessageToJsonObjectz"_Printer._ValueMessageToJsonObject]s   v & &E }-- T   / /0B C CC "e E  >=>> > E  >=>> >>gu%%e)8?  " "#3U ; ;;rc*fd|jDS)zBConverts ListValue message according to Proto3 JSON Specification.c:g|]}|Sr)r)rOrJr;s rrQz:_Printer._ListValueMessageToJsonObject..us7 ) ) )  * *5 1 1 ) ) )r)valuesrs` rrz&_Printer._ListValueMessageToJsonObjectss0 ) ) ) )  ) ) ))rc^|j}i}|D] }|||||<!|S)z?Converts Struct message according to Proto3 JSON Specification.)r\r)r;rr\retrds r_StructMessageToJsonObjectz#_Printer._StructMessageToJsonObjectxs? ^F C==//s <>>@***X===*""" <<<,))) CCCCCrrc"|jjdkS)Nzgoogle/protobuf/wrappers.proto)filerS)rHs rrDrDs  %)I IIrcpi}|D]0\}}||vr"td||||<1|S)Nz'Failed to load JSON: duplicate key {0}.)rr9)r@resultrSrJs r_DuplicateCheckerrsP &kdE v~~ @GGMM N NNF4LL -rcHtj}||jn|}|dd} ||}n5#t $r(}t d||d}~wwxYwtj |}|S)z"Creates a message from a type URL.N/z0Can not find message descriptor by type_url: {0}) rDefaultpoolsplitFindMessageTypeByNameKeyError TypeErrorr9rGetMessageClass)rr%dbr type_namerHrf message_classs rrrs  "#+$nnS!!"%)33I>>  :AA(KK   "12DEE- sA B#A>>Bdc:t|ts|d} tj|t }nB#t $r5}tdt||d}~wwxYwt|||||S)alParses a JSON representation of a protocol message into a message. Args: text: Message JSON representation. message: A protocol buffer message to merge into. ignore_unknown_fields: If True, do not raise errors for unknown fields. descriptor_pool: A Descriptor Pool for resolving types. If None use the default. max_recursion_depth: max recursion depth of JSON message to be deserialized. JSON messages over this depth will fail to be deserialized. Default value is 100. Returns: The same message passed as argument. Raises:: ParseError: On JSON parsing problems. ri)object_pairs_hookzFailed to load JSON: {0}.N) rVrXrsr>loadsrrarr9 ParseDict)textrignore_unknown_fieldsr%max_recursion_depthr@rfs rParsers. D#   ;;w  DH D,= > > >BB HHH 077A?? @ @aGH 2w 5& ( ((sA B0BBcVt|||}|||d|S)a8Parses a JSON dictionary representation into a message. Args: js_dict: Dict representation of a JSON message. message: A protocol buffer message to merge into. ignore_unknown_fields: If True, do not raise errors for unknown fields. descriptor_pool: A Descriptor Pool for resolving types. If None use the default. max_recursion_depth: max recursion depth of JSON message to be deserialized. JSON messages over this depth will fail to be deserialized. Default value is 100. Returns: The same message passed as argument. )_ParserConvertMessage)js_dictrrr%rparsers rrrs4( (/;N O O&"--- .rcNeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d S) rz(JSON format parser for protocol message.c>||_||_||_d|_dS)Nr)rr%rrecursion_depth)r;rr%rs rr<z_Parser.__init__s(!6D*D2DDrc|xjdz c_|j|jkr'td|j|j}|j}|s|j}t|r||||nM|tvr-tt|d||||n| ||||xjdzc_dS)aConvert a JSON object into a message. Args: value: A JSON object. message: A WKT or regular protocol message to record the data. path: parent path to log parse error info. Raises: ParseError: In case of convert problems. z,Message too deep. Max recursion depth is {0}N) rrrr9rBrCrSrD_ConvertWrapperMessagerFr_ConvertFieldValuePair)r;rJrpathrHrCs rrz_Parser.ConvertMessages  A d666 ELL  "$$ % %% +",I %  $d+,,8 !!%$7777 o % %Gl?9-a0%$GGMMMM !!%$777Arc  g}|j}td|jD}|D]=} ||d}|s|j|d}|st |r|js(td |j ||dd} |j | }|sJd | ddd} |j | }|sA|jrtd |j ||d|jD||vr.td |jj ||||||} |jU| S|jj} | |vr.td |jj | ||| | |jt(jjkr-|jj d krt1||j} d | _n[|jt(jjkr'|jj d krt9||jd n||jt=|rH||j|| ||d ||n|j t(jj!kr||jtE| tFs$td || ||jt(jjkrtI| D]\} }t1||j%} |4| jj d kr$td ||| |&|| d ||| nztI| D]w\} }|$td ||| t1||jtO||d ||| xn|jt(jjkrj|j(r|j |} nt1||j} | )|&| | d ||nn|j(r.tO| |d |||j |<n9t9||jtO| |d ||#t$rO}|r+|j$td |||ttU||d}~wtV$r)}td |||d}~wtX$r)}td |||d}~wwxYwdS)a'Convert field value pairs into regular message. Args: js: A JSON object to convert the field value pairs. message: A regular protocol message to record the data. path: parent path to log parse error info. Raises: ParseError: In case of problems converting. c3(K|] }|j|fVdSr-rTrOfs r z1_Parser._ConvertFieldValuePair..sECC#$!" Q/CCCCCCrNz0Message type {0} does not have extensions at {1}rr.zaMessage type "{0}" has no field named "{1}" at "{2}". Available Fields(except extensions): "{3}"cg|] }|j Srrrs rrQz2_Parser._ConvertFieldValuePair...sCCCAAKCCCrzBMessage type "{0}" should not have multiple "{1}" fields at "{2}".zHMessage type "{0}" should not have multiple "{1}" oneof fields at "{2}".google.protobuf.Valuerrh{0}.{1}z4repeated field {0} must be in [] which is {1} at {2}zPnull is not allowed to be used as an element in a repeated field at {0}.{1}[{2}]z {0}.{1}[{2}]zFailed to parse {0} field: {1}.)-rBdictr\rmrU_VALID_EXTENSION_NAMEmatch is_extendablerr9rC Extensions_FindExtensionByNamejoinrrappendr_rSr]rr/r^r1rrrjrksetattr ClearFieldr6_ConvertMapFieldValuerYrZrVlist enumerateaddr_ConvertScalarFieldValuer[ SetInParentrXrar)r;r@rrnamesrHfields_by_json_namerSr5 identifierrJ oneof_namerindexitemrfs rrz_Parser._ConvertFieldValuePairsn E +CC(:(ACCCCCwwv#''d33 D$377dCC% H.44T:: H#1 9BII&0$8899 9AbDz*$99*EE% H*"2"23"7"7"<==J&;;JGGE F  '  =>Df%/tCC);)BCCC?E?EFF F 5==44:F&1;T45I5IJJ J  T4  ! -%2C-2* 5 <>k,(26MMM "HHN%)4I8I8999!!$ "0"7"7dE"J"JLLLL L )// N N t "HHN%)4I8I8999guz**11*E>#8#8tU#K#KMMNNNN  N^z9I I I   7!,U3KK!'5:66K  ! ! # # #   e[)2B2B42N2N O O O O   H(@ui..tT::)<) >@@@@@@rc  t|ts#td||||D]>}||||j|d||?dS)z2Convert a JSON representation into Struct message.z,Struct must be in a dict which is {0} at {1}rN)rVrrr9Clearrr\)r;rJrrrds rrz_Parser._ConvertStructMessages eT " " ELL    MMOOO== c GN3,? ) 0 0s ; ;==== Frc |jjd}t|dt||d|dS)z3Convert a JSON representation into Wrapper message.rJrrN)rBrUrrr9)r;rJrrr5s rrz_Parser._ConvertWrapperMessagesX   -g 6E  K4F4Ft4L4LMMMOOOOOrc lt|ts)td|j|||jjd}|jjd}|D]}t||d|d}|jtj j krK| ||t||j|d||t|||d||t||j|<dS) afConvert map field value for a message map field. Args: value: A JSON object to convert the map field value. message: A protocol message to record the converted data. field: The descriptor of the map field to be converted. path: parent path to log parse error info. Raises: ParseError: In case of convert problems. z3Map field {0} must be in a dict which is {1} at {2}rdrJz{0}.keyTrrN)rVrrr9rSr1rUrr]rr/r^rr) r;rJrr5r key_field value_fieldrd key_values rrz_Parser._ConvertMapFieldValuesV eT " "(  ? F Fj% ' ' ( (("1%8I$3G> @ @ @ @3K #J **;*;D)*L*L3N3N3N$$Y// N NrN)rrrrr<rrrrrrrrrrrrrrs006FFFP   @<<<%%%$@@@    OOONNNNNrrc |jtvrt|S|jtvrt ||S|jt jjkrt||S|jt jj kr|j t jj kr\t|tr|d}n|}|ddt|dzz zz}t!j|St$|rt)d|S|jt jjkr|jj|d}| t3|}|jj|d}n@#t6$r3}t)d||jj|d}~wwxYw|;|jjr-t)d||jj|S|jSdS#t($r)}t)d|||d}~wwxYw)aqConvert a single scalar field value. Args: value: A scalar value to convert the scalar field value. field: The descriptor of the field to convert. path: parent path to log parse error info. require_str: If True, the field value must be a str. Returns: The converted scalar field value Raises: ParseError: In case of convert problems. ri=zUnpaired surrogateNz(Invalid enum value {0} for enum type {1}r) r] _INT_TYPES_ConvertIntegerrv _ConvertFloatrr/rt _ConvertBoolror.rprVrXencodelenrqurlsafe_b64decode_UNPAIRED_SURROGATE_PATTERNsearchrrjrkvalues_by_namermintrlrar9rCrnnumber) rJr5r require_strencoded padded_valuerrrfs rrrs\(: ~## U # ## < ' ' 5% ( (( :5B B B % - -- :5D D D z1< < < eS ! ! LL))'''S\\A-=)=!>> ' 555 ' - -e 4 4 1/00 0 :5B B B?155eTBBj   8u::&7;;FDII** 8 8 8ELLU_.001167 8 8   _ & GNNu02233 3M  ! C B" ::: \((D11 2 29:s^HH)H'BH7*H"|tj krt d|tj krt d|dkrt d t|S#t$r}|tkrtd cYd }~S|tkrtd cYd }~S|t krtdcYd }~St d ||d }~wwxYw) z!Convert an floating point number.z,Couldn't parse NaN, use quoted "NaN" insteadrzICouldn't parse Infinity or value too large, use quoted "Infinity" insteadzKCouldn't parse -Infinity or value too small, use quoted "-Infinity" insteadzFloat value too largezFloat value too smallnanz-Couldn't parse float "nan", use "NaN" insteadz-infNinfzCouldn't parse float: {0})rVr~rwr{rrxr]rr/r}r _FLOAT_MAX _FLOAT_MINraryrzr|r9)rJr5rfs rrrUsu2 z%H F G GG z%; 9:: ::;; ; ~3AAA ) ) )0111 ) ) )0111 e^^ E F FF J <<  J J J  6]] )   5\\ $ 5\\ 3::5AA B BI Js<C.. E=8E8E=E80E=6E8E=#E88E=c|r2|dkrdS|dkrdStd|t|tstd|S)zConvert a boolean value. Args: value: A scalar value to convert. require_str: If True, value must be a str. Returns: The bool parsed. Raises: ParseError: If a boolean value couldn't be consumed. rKTrLFz#Expected "true" or "false", not {0}z%Expected true or false without quotes)rr9rVrW)rJrs rrrysnL  T '   U <CCEJJ K KK E4 > < = == ,rrrrrrrrrrr)zgoogle.protobuf.Anyzgoogle.protobuf.Durationzgoogle.protobuf.FieldMaskzgoogle.protobuf.ListValuezgoogle.protobuf.Structzgoogle.protobuf.Timestampr)FFrFFNNTr)FNr)F):r __author__rq collectionsrr>rwoperatorrresysgoogle.protobuf.internalrgoogle.protobufrrr_TIMESTAMPFOMAT frozensetr/ CPPTYPE_INT32CPPTYPE_UINT32 CPPTYPE_INT64CPPTYPE_UINT64rrur}CPPTYPE_DOUBLErvrzryr|compiler r Exceptionr rrr(r+r6objectrrDrrrrrr~rrrrrrrFrrrr.s>  +  ###### !!!!!! 222222&&&&&&++++++++++++& Y 2@"2A"2@"2ACDD y*4B$4CEFF y*4B$4CEFF    (bjMOO# #:;;00000I000/////5///)))))))) $) %  )H)H)H)H\$) %  #/#/#/#/L555 OCOCOCOCOCvOCOCOCdJJJ   "!&! ((((F%*""% 2e aNaNaNaNaNfaNaNaNH 7:7:7:7:t2!J!J!JH4602!>!9!;"?":"<"A"<">;68"?":"<946r