1515 */
1616package com .google .cloud .bigquery .storage .v1alpha2 ;
1717
18+ import com .google .common .collect .ImmutableMap ;
1819import com .google .protobuf .Descriptors .Descriptor ;
1920import com .google .protobuf .Descriptors .FieldDescriptor ;
2021import com .google .protobuf .DynamicMessage ;
2930
3031/** Converts Json data to protocol buffer messages given the protocol buffer descriptor. */
3132public class JsonToProtoMessage {
33+ private static ImmutableMap <FieldDescriptor .Type , String > FieldTypeToDebugMessage =
34+ new ImmutableMap .Builder <FieldDescriptor .Type , String >()
35+ .put (FieldDescriptor .Type .BOOL , "boolean" )
36+ .put (FieldDescriptor .Type .BYTES , "string" )
37+ .put (FieldDescriptor .Type .INT32 , "int32" )
38+ .put (FieldDescriptor .Type .DOUBLE , "double" )
39+ .put (FieldDescriptor .Type .INT64 , "int64" )
40+ .put (FieldDescriptor .Type .STRING , "string" )
41+ .put (FieldDescriptor .Type .MESSAGE , "object" )
42+ .build ();
3243
3344 /**
3445 * Converts Json data to protocol buffer messages given the protocol buffer descriptor.
@@ -44,7 +55,8 @@ public static DynamicMessage convertJsonToProtoMessage(
4455 if (json .length () == 0 ) {
4556 throw new IllegalArgumentException ("JSONObject is empty." );
4657 }
47- return convertJsonToProtoMessageImpl (protoSchema , json , "root" , true , allowUnknownFields );
58+ return convertJsonToProtoMessageImpl (
59+ protoSchema , json , "root" , /*topLevel=*/ true , allowUnknownFields );
4860 }
4961
5062 /**
@@ -73,21 +85,19 @@ private static DynamicMessage convertJsonToProtoMessageImpl(
7385 protoFieldNames .add (field .getName ());
7486 }
7587
76- HashMap <String , String > jsonLowercaseNameToName = new HashMap <String , String >();
88+ HashMap <String , String > jsonLowercaseNameToJsonName = new HashMap <String , String >();
7789 String [] jsonNames = JSONObject .getNames (json );
7890 for (int i = 0 ; i < jsonNames .length ; i ++) {
79- jsonLowercaseNameToName .put (jsonNames [i ].toLowerCase (), jsonNames [i ]);
91+ jsonLowercaseNameToJsonName .put (jsonNames [i ].toLowerCase (), jsonNames [i ]);
8092 }
8193
8294 if (!allowUnknownFields ) {
8395 for (int i = 0 ; i < jsonNames .length ; i ++) {
8496 if (!protoFieldNames .contains (jsonNames [i ])) {
8597 throw new IllegalArgumentException (
86- "JSONObject has fields unknown to BigQuery: "
87- + jsonScope
88- + "."
89- + jsonNames [i ]
90- + ". Set allowUnknownFields to True to allow unknown fields." );
98+ String .format (
99+ "JSONObject has fields unknown to BigQuery: %s.%s. Set allowUnknownFields to True to allow unknown fields." ,
100+ jsonScope , jsonNames [i ]));
91101 }
92102 }
93103 }
@@ -97,7 +107,7 @@ private static DynamicMessage convertJsonToProtoMessageImpl(
97107 String lowercaseFieldName = field .getName ().toLowerCase ();
98108 String currentScope = jsonScope + "." + field .getName ();
99109
100- if (!jsonLowercaseNameToName .containsKey (lowercaseFieldName )) {
110+ if (!jsonLowercaseNameToJsonName .containsKey (lowercaseFieldName )) {
101111 if (field .isRequired ()) {
102112 throw new IllegalArgumentException (
103113 "JSONObject does not have the required field " + currentScope + "." );
@@ -111,15 +121,15 @@ private static DynamicMessage convertJsonToProtoMessageImpl(
111121 protoMsg ,
112122 field ,
113123 json ,
114- jsonLowercaseNameToName .get (lowercaseFieldName ),
124+ jsonLowercaseNameToJsonName .get (lowercaseFieldName ),
115125 currentScope ,
116126 allowUnknownFields );
117127 } else {
118128 fillRepeatedField (
119129 protoMsg ,
120130 field ,
121131 json ,
122- jsonLowercaseNameToName .get (lowercaseFieldName ),
132+ jsonLowercaseNameToJsonName .get (lowercaseFieldName ),
123133 currentScope ,
124134 allowUnknownFields );
125135 }
@@ -151,78 +161,63 @@ private static void fillField(
151161 boolean allowUnknownFields )
152162 throws IllegalArgumentException {
153163 java .lang .Object val ;
154- switch (fieldDescriptor .getType ()) {
155- case BOOL :
156- try {
164+ String error ;
165+ try {
166+ switch (fieldDescriptor .getType ()) {
167+ case BOOL :
157168 protoMsg .setField (fieldDescriptor , new Boolean (json .getBoolean (actualJsonKeyName )));
158- } catch (JSONException e ) {
159- throw new IllegalArgumentException (
160- "JSONObject does not have a boolean field at " + currentScope + "." );
161- }
162- break ;
163- case BYTES :
164- try {
169+ break ;
170+ case BYTES :
165171 protoMsg .setField (fieldDescriptor , json .getString (actualJsonKeyName ).getBytes ());
166- } catch (JSONException e ) {
167- throw new IllegalArgumentException (
168- "JSONObject does not have a string field at " + currentScope + "." );
169- }
170- break ;
171- case INT64 :
172- val = json .get (actualJsonKeyName );
173- if (val instanceof Integer ) {
174- protoMsg .setField (fieldDescriptor , new Long ((Integer ) val ));
175- } else if (val instanceof Long ) {
176- protoMsg .setField (fieldDescriptor , new Long ((Long ) val ));
177- } else {
178- throw new IllegalArgumentException (
179- "JSONObject does not have a int64 field at " + currentScope + "." );
180- }
181- break ;
182- case INT32 :
183- val = json .get (actualJsonKeyName );
184- if (val instanceof Integer ) {
185- protoMsg .setField (fieldDescriptor , new Integer ((Integer ) val ));
186- } else {
187- throw new IllegalArgumentException (
188- "JSONObject does not have a int32 field at " + currentScope + "." );
189- }
190- break ;
191- case STRING :
192- try {
172+ break ;
173+ case INT64 :
174+ val = json .get (actualJsonKeyName );
175+ if (val instanceof Integer ) {
176+ protoMsg .setField (fieldDescriptor , new Long ((Integer ) val ));
177+ } else if (val instanceof Long ) {
178+ protoMsg .setField (fieldDescriptor , new Long ((Long ) val ));
179+ } else {
180+ throw new JSONException ("" );
181+ }
182+ break ;
183+ case INT32 :
184+ val = json .get (actualJsonKeyName );
185+ if (val instanceof Integer ) {
186+ protoMsg .setField (fieldDescriptor , new Integer ((Integer ) val ));
187+ } else {
188+ throw new JSONException ("" );
189+ }
190+ break ;
191+ case STRING :
193192 protoMsg .setField (fieldDescriptor , json .getString (actualJsonKeyName ));
194- } catch (JSONException e ) {
195- throw new IllegalArgumentException (
196- "JSONObject does not have a string field at " + currentScope + "." );
197- }
198- break ;
199- case DOUBLE :
200- val = json .get (actualJsonKeyName );
201- if (val instanceof Double ) {
202- protoMsg .setField (fieldDescriptor , new Double ((double ) val ));
203- } else if (val instanceof Float ) {
204- protoMsg .setField (fieldDescriptor , new Double ((float ) val ));
205- } else {
206- throw new IllegalArgumentException (
207- "JSONObject does not have a double field at " + currentScope + "." );
208- }
209- break ;
210- case MESSAGE :
211- Message .Builder message = protoMsg .newBuilderForField (fieldDescriptor );
212- try {
193+ break ;
194+ case DOUBLE :
195+ val = json .get (actualJsonKeyName );
196+ if (val instanceof Double ) {
197+ protoMsg .setField (fieldDescriptor , new Double ((double ) val ));
198+ } else if (val instanceof Float ) {
199+ protoMsg .setField (fieldDescriptor , new Double ((float ) val ));
200+ } else {
201+ throw new JSONException ("" );
202+ }
203+ break ;
204+ case MESSAGE :
205+ Message .Builder message = protoMsg .newBuilderForField (fieldDescriptor );
213206 protoMsg .setField (
214207 fieldDescriptor ,
215208 convertJsonToProtoMessageImpl (
216209 fieldDescriptor .getMessageType (),
217210 json .getJSONObject (actualJsonKeyName ),
218211 currentScope ,
219- false ,
212+ /*topLevel =*/ false ,
220213 allowUnknownFields ));
221- } catch (JSONException e ) {
222- throw new IllegalArgumentException (
223- "JSONObject does not have a object field at " + currentScope + "." );
224- }
225- break ;
214+ break ;
215+ }
216+ } catch (JSONException e ) {
217+ throw new IllegalArgumentException (
218+ String .format (
219+ "JSONObject does not have a %s field at %s." ,
220+ FieldTypeToDebugMessage .get (fieldDescriptor .getType ()), currentScope ));
226221 }
227222 }
228223
@@ -261,12 +256,8 @@ private static void fillRepeatedField(
261256 protoMsg .addRepeatedField (fieldDescriptor , new Boolean (jsonArray .getBoolean (i )));
262257 } catch (JSONException e ) {
263258 throw new IllegalArgumentException (
264- "JSONObject does not have a boolean field at "
265- + currentScope
266- + "["
267- + i
268- + "]"
269- + "." );
259+ String .format (
260+ "JSONObject does not have a boolean field at %s[%d]." , currentScope , i ));
270261 }
271262 }
272263 break ;
@@ -276,7 +267,8 @@ private static void fillRepeatedField(
276267 protoMsg .addRepeatedField (fieldDescriptor , jsonArray .getString (i ).getBytes ());
277268 } catch (JSONException e ) {
278269 throw new IllegalArgumentException (
279- "JSONObject does not have a string field at " + currentScope + "[" + i + "]" + "." );
270+ String .format (
271+ "JSONObject does not have a string field at %s[%d]." , currentScope , i ));
280272 }
281273 }
282274 break ;
@@ -289,7 +281,8 @@ private static void fillRepeatedField(
289281 protoMsg .addRepeatedField (fieldDescriptor , new Long ((Long ) val ));
290282 } else {
291283 throw new IllegalArgumentException (
292- "JSONObject does not have a int64 field at " + currentScope + "[" + i + "]" + "." );
284+ String .format (
285+ "JSONObject does not have a int64 field at %s[%d]." , currentScope , i ));
293286 }
294287 }
295288 break ;
@@ -300,7 +293,8 @@ private static void fillRepeatedField(
300293 protoMsg .addRepeatedField (fieldDescriptor , new Integer ((Integer ) val ));
301294 } else {
302295 throw new IllegalArgumentException (
303- "JSONObject does not have a int32 field at " + currentScope + "[" + i + "]" + "." );
296+ String .format (
297+ "JSONObject does not have a int32 field at %s[%d]." , currentScope , i ));
304298 }
305299 }
306300 break ;
@@ -310,7 +304,8 @@ private static void fillRepeatedField(
310304 protoMsg .addRepeatedField (fieldDescriptor , jsonArray .getString (i ));
311305 } catch (JSONException e ) {
312306 throw new IllegalArgumentException (
313- "JSONObject does not have a string field at " + currentScope + "[" + i + "]" + "." );
307+ String .format (
308+ "JSONObject does not have a string field at %s[%d]." , currentScope , i ));
314309 }
315310 }
316311 break ;
@@ -323,7 +318,8 @@ private static void fillRepeatedField(
323318 protoMsg .addRepeatedField (fieldDescriptor , new Double ((float ) val ));
324319 } else {
325320 throw new IllegalArgumentException (
326- "JSONObject does not have a double field at " + currentScope + "[" + i + "]" + "." );
321+ String .format (
322+ "JSONObject does not have a double field at %s[%d]." , currentScope , i ));
327323 }
328324 }
329325 break ;
@@ -337,11 +333,12 @@ private static void fillRepeatedField(
337333 fieldDescriptor .getMessageType (),
338334 jsonArray .getJSONObject (i ),
339335 currentScope ,
340- false ,
336+ /*topLevel =*/ false ,
341337 allowUnknownFields ));
342338 } catch (JSONException e ) {
343339 throw new IllegalArgumentException (
344- "JSONObject does not have a object field at " + currentScope + "[" + i + "]" + "." );
340+ String .format (
341+ "JSONObject does not have a object field at %s[%d]." , currentScope , i ));
345342 }
346343 }
347344 break ;
0 commit comments