Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Make all classes serializable to JSON objects #10

@nathancarter

Description

@nathancarter

Use case: Fetch a large group of matches and want to store them somewhere for later analysis offline. Want to be able to convert a match (with all included player, roster, participant, and asset objects) to JSON that can be written to a file, then deserialized back into the same hierarchy of Match/Player/Roster/Participant/Asset objects later. For bonus points, include telemetry data.

I include here some CoffeeScript code that seems to work, but native support is better than my hacking, of course.

vgObjectToJSON = ( object ) ->
    result = data : object.data
    for own name, ctor of vg.models
        if name isnt 'Base' and object instanceof ctor
            result.ctor = name
    for relationship in object.relationships ? [ ]
        type = relationship.type
        subobj = object[type]
        if subobj instanceof Array
            result[type] = ( vgObjectToJSON s for s in subobj )
        else
            result[type] = vgObjectToJSON subobj
    # in case I've fetched and embedded telemetry earlier:
    result.telemetry = object.telemetry
    result
vgObjectFromJSON = ( json ) ->
    if typeof json is 'string' then json = JSON.parse json
    ctor = vg.models[json.ctor]
    delete json.ctor
    telemetry = json.telemetry
    delete json.telemetry
    result = new ctor json.data
    for relationship in result.relationships ? [ ]
        type = relationship.type
        subobj = json[type]
        if subobj instanceof Array
            result[type] = ( vgObjectFromJSON s for s in subobj )
        else
            result[type] = vgObjectFromJSON subobj
    result.telemetry = telemetry
    result

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions