beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dhalp...@apache.org
Subject [1/2] incubator-beam git commit: Updates json to/from Python object conversion to properly handle None values.
Date Tue, 02 Aug 2016 18:05:02 GMT
Repository: incubator-beam
Updated Branches:
  refs/heads/python-sdk fa302a3be -> e834fa82b


Updates json to/from Python object  conversion to properly handle None values.


Project: http://git-wip-us.apache.org/repos/asf/incubator-beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-beam/commit/3a4f2234
Tree: http://git-wip-us.apache.org/repos/asf/incubator-beam/tree/3a4f2234
Diff: http://git-wip-us.apache.org/repos/asf/incubator-beam/diff/3a4f2234

Branch: refs/heads/python-sdk
Commit: 3a4f2234e36791fd64125d67dbedaebbc2a981b1
Parents: fa302a3
Author: Chamikara Jayalath <chamikara@google.com>
Authored: Mon Aug 1 23:12:10 2016 -0700
Committer: Chamikara Jayalath <chamikara@google.com>
Committed: Mon Aug 1 23:12:10 2016 -0700

----------------------------------------------------------------------
 sdks/python/apache_beam/internal/json_value.py  | 20 +++++++++++---------
 .../apache_beam/internal/json_value_test.py     |  6 ++++++
 2 files changed, 17 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/3a4f2234/sdks/python/apache_beam/internal/json_value.py
----------------------------------------------------------------------
diff --git a/sdks/python/apache_beam/internal/json_value.py b/sdks/python/apache_beam/internal/json_value.py
index df9d785..b7f6cc0 100644
--- a/sdks/python/apache_beam/internal/json_value.py
+++ b/sdks/python/apache_beam/internal/json_value.py
@@ -50,7 +50,7 @@ def to_json_value(obj, with_type=False):
   """Converts Python objects into extra_types.JsonValue objects.
 
   Args:
-    obj: Python object to be converted.
+    obj: Python object to be converted. Can be 'None'.
     with_type: If true then the basic types (string, int, float, bool) will
       be wrapped in @type/value dictionaries. Otherwise the straight value is
       encoded into a JsonValue.
@@ -62,11 +62,13 @@ def to_json_value(obj, with_type=False):
   Raises:
     TypeError: if the Python object contains a type that is not supported.
 
-  The types supported are str, bool, list, tuple, dict. The Dataflow API
-  requires JsonValue(s) in many places, and it is quite convenient to be able
-  to specify these hierarchical objects using Python syntax.
+  The types supported are str, bool, list, tuple, dict, and None. The Dataflow
+  API requires JsonValue(s) in many places, and it is quite convenient to be
+  able to specify these hierarchical objects using Python syntax.
   """
-  if isinstance(obj, (list, tuple)):
+  if obj is None:
+    return extra_types.JsonValue(is_null=True)
+  elif isinstance(obj, (list, tuple)):
     return extra_types.JsonValue(
         array_value=extra_types.JsonArray(
             entries=[to_json_value(o, with_type=with_type) for o in obj]))
@@ -104,9 +106,9 @@ def from_json_value(v):
   Raises:
     TypeError: if the JsonValue object contains a type that is not supported.
 
-  The types supported are str, bool, list, dict. The Dataflow API returns
-  JsonValue(s) in many places and it is quite convenient to be able to convert
-  these hierarchical objects to much simpler Python objects.
+  The types supported are str, bool, list, dict, and None. The Dataflow API
+  returns JsonValue(s) in many places and it is quite convenient to be able to
+  convert these hierarchical objects to much simpler Python objects.
   """
   if isinstance(v, extra_types.JsonValue):
     if v.string_value is not None:
@@ -122,7 +124,7 @@ def from_json_value(v):
     elif v.object_value is not None:
       return from_json_value(v.object_value)
     elif v.is_null:
-      return []
+      return None
   elif isinstance(v, extra_types.JsonArray):
     return [from_json_value(e) for e in v.entries]
   elif isinstance(v, extra_types.JsonObject):

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/3a4f2234/sdks/python/apache_beam/internal/json_value_test.py
----------------------------------------------------------------------
diff --git a/sdks/python/apache_beam/internal/json_value_test.py b/sdks/python/apache_beam/internal/json_value_test.py
index ad2a394..b3d9e83 100644
--- a/sdks/python/apache_beam/internal/json_value_test.py
+++ b/sdks/python/apache_beam/internal/json_value_test.py
@@ -41,6 +41,9 @@ class JsonValueTest(unittest.TestCase):
   def test_float_to(self):
     self.assertEquals(JsonValue(double_value=2.75), to_json_value(2.75))
 
+  def test_none_to(self):
+    self.assertEquals(JsonValue(is_null=True), to_json_value(None))
+
   def test_string_from(self):
     self.assertEquals('WXYZ', from_json_value(to_json_value('WXYZ')))
 
@@ -61,6 +64,9 @@ class JsonValueTest(unittest.TestCase):
     self.assertEquals('http://schema.org/Text', rt['@type'])
     self.assertEquals('abcd', rt['value'])
 
+  def test_none_from(self):
+    self.assertIsNone(from_json_value(to_json_value(None)))
+
 
 if __name__ == '__main__':
   unittest.main()


Mime
View raw message