From 9580aeb223c5368ef07afe6a1beb43f708a63742 Mon Sep 17 00:00:00 2001
From: George Macon <george.macon@gtri.gatech.edu>
Date: Wed, 28 Nov 2012 12:15:39 -0500
Subject: [PATCH] Use two-char hex for binary type

This matches the Mongo Extended JSON spec.
---
 bson/json_util.py |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/bson/json_util.py b/bson/json_util.py
index da4d065..bd3eff7 100644
--- a/bson/json_util.py
+++ b/bson/json_util.py
@@ -32,14 +32,14 @@ Example usage (serialization)::
    ...        {'bar': {'hello': 'world'}},
    ...        {'code': Code("function x() { return 1; }")},
    ...        {'bin': Binary("\x00\x01\x02\x03\x04")}])
-   '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$scope": {}, "$code": "function x() { return 1; }"}}, {"bin": {"$type": 0, "$binary": "AAECAwQ=\\n"}}]'
+   '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$scope": {}, "$code": "function x() { return 1; }"}}, {"bin": {"$type": "00", "$binary": "AAECAwQ=\\n"}}]'
 
 Example usage (deserialization)::
 
 .. doctest::
 
    >>> from bson.json_util import loads
-   >>> loads('[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$scope": {}, "$code": "function x() { return 1; }"}}, {"bin": {"$type": 0, "$binary": "AAECAwQ=\\n"}}]')
+   >>> loads('[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$scope": {}, "$code": "function x() { return 1; }"}}, {"bin": {"$type": "00", "$binary": "AAECAwQ=\\n"}}]')
    [{u'foo': [1, 2]}, {u'bar': {u'hello': u'world'}}, {u'code': Code('function x() { return 1; }', {})}, {u'bin': Binary('\x00\x01\x02\x03\x04', 0)}]
 
 Alternatively, you can manually pass the `default` to :func:`json.dumps`.
@@ -151,7 +151,8 @@ def object_hook(dct):
     if "$maxKey" in dct:
         return MaxKey()
     if "$binary" in dct:
-        return Binary(base64.b64decode(dct["$binary"].encode()), dct["$type"])
+        return Binary(base64.b64decode(dct["$binary"].encode()),
+                      int(dct["$type"], 16))
     if "$code" in dct:
         return Code(dct["$code"], dct.get("$scope"))
     if bson.has_uuid() and "$uuid" in dct:
@@ -189,10 +190,10 @@ def default(obj):
         return {'$code': "%s" % obj, '$scope': obj.scope}
     if isinstance(obj, Binary):
         return {'$binary': base64.b64encode(obj).decode(),
-                '$type': obj.subtype}
+                '$type': "%02x" % obj.subtype}
     if PY3 and isinstance(obj, binary_type):
         return {'$binary': base64.b64encode(obj).decode(),
-                '$type': 0}
+                '$type': "00"}
     if bson.has_uuid() and isinstance(obj, bson.uuid.UUID):
         return {"$uuid": obj.hex}
     raise TypeError("%r is not JSON serializable" % obj)
-- 
1.7.9.5

