Skip to content

Instantly share code, notes, and snippets.

@Gsantomaggio
Last active October 31, 2022 08:27
Show Gist options
  • Select an option

  • Save Gsantomaggio/921d2de9d1c751f6b3b4bfecca4e6a9d to your computer and use it in GitHub Desktop.

Select an option

Save Gsantomaggio/921d2de9d1c751f6b3b4bfecca4e6a9d to your computer and use it in GitHub Desktop.

Revisions

  1. Gsantomaggio revised this gist Oct 30, 2022. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions ser.py
    Original file line number Diff line number Diff line change
    @@ -24,11 +24,11 @@ def default(self, o):


    start_time = time.time()
    x = 100_000
    x = 1_000_000
    for n in range(x):
    foo_nested = FooWithNested(x + 100, x, 123)
    foo = Foo("String_1", "string_2", foo_nested)
    json_string = FooEncoder().encode(foo)
    foo = Foo("String_1", "string_2", 123)
    foo_nested = FooWithNested(n + 100, n, foo)
    json_string = FooEncoder().encode(foo_nested)
    obj = json.loads(json_string, cls=json.JSONDecoder)


  2. Gsantomaggio revised this gist Oct 30, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ser.py
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,6 @@ def default(self, o):
    foo_nested = FooWithNested(x + 100, x, 123)
    foo = Foo("String_1", "string_2", foo_nested)
    json_string = FooEncoder().encode(foo)
    employeeJSONData = json.dumps(foo, indent=4, cls=FooEncoder)
    obj = json.loads(json_string, cls=json.JSONDecoder)


  3. Gsantomaggio created this gist Oct 30, 2022.
    37 changes: 37 additions & 0 deletions ser.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    import json
    import time
    from json import JSONEncoder


    class Foo:
    def __init__(self, string_1, string_2, int_1):
    self.string_1 = string_1
    self.string_1 = string_2
    self.int_1 = int_1


    class FooWithNested:
    def __init__(self, int64_2, int64_1, foo_nested_):
    self.int64_2 = int64_2
    self.int64_1 = int64_1
    self.foo_nested = foo_nested_


    # subclass JSONEncoder
    class FooEncoder(JSONEncoder):
    def default(self, o):
    return o.__dict__


    start_time = time.time()
    x = 100_000
    for n in range(x):
    foo_nested = FooWithNested(x + 100, x, 123)
    foo = Foo("String_1", "string_2", foo_nested)
    json_string = FooEncoder().encode(foo)
    employeeJSONData = json.dumps(foo, indent=4, cls=FooEncoder)
    obj = json.loads(json_string, cls=json.JSONDecoder)


    txt2 = "Seconds: {0}, iterations: {1}".format((time.time() - start_time), x)
    print(txt2)