ndio/ramon/__init__.py
def to_json(ramons, flatten=False):
    """
    Converts RAMON objects into a JSON string which can be directly written out
    to a .json file. You can pass either a single RAMON or a list. If you pass
    a single RAMON, it will still be exported with the ID as the key. In other
    words:

        type(from_json(to_json(ramon))) # ALWAYS returns a list

    ...even if `type(ramon)` is a RAMON, not a list.

    Arguments:
        ramons (RAMON or list): The RAMON object(s) to convert to JSON.
        flatten (bool : False): If ID should be used as a key. If not, then
            a single JSON document is returned.

    Returns:
        str: The JSON representation of the RAMON objects, in the schema:
            ```
            {
                <id>: {
                    type: . . . ,
                    metadata: {
                        . . .
                    }
                },
            }
            ```

    Raises:
        ValueError: If an invalid RAMON is passed.
    """
    if type(ramons) is not list:
        ramons = [ramons]

    out_ramons = {}
    for r in ramons:
        out_ramons[r.id] = {
            "id": r.id,
            "type": _reverse_ramon_types[type(r)],
            "metadata": vars(r)
        }

    if flatten:
        return jsonlib.dumps(out_ramons.values()[0])

    return jsonlib.dumps(out_ramons)
def to_json(ramons, flatten=False):
    """
    Converts RAMON objects into a JSON string which can be directly written out
    to a .json file. You can pass either a single RAMON or a list. If you pass
    a single RAMON, it will still be exported with the ID as the key. In other
    words:

        type(from_json(to_json(ramon))) # ALWAYS returns a list

    ...even if `type(ramon)` is a RAMON, not a list.

    Arguments:
        ramons (RAMON or list): The RAMON object(s) to convert to JSON.
        flatten (bool : False): If ID should be used as a key. If not, then
            a single JSON document is returned.

    Returns:
        str: The JSON representation of the RAMON objects, in the schema:
            ```
            {
                <id>: {
                    type: . . . ,
                    metadata: {
                        . . .
                    }
                },
            }
            ```

    Raises:
        ValueError: If an invalid RAMON is passed.
    """
    if type(ramons) is not list:
        ramons = [ramons]

    out_ramons = {}
    for r in ramons:
        out_ramons[r.id] = {
            "id": r.id,
            "type": _reverse_ramon_types[type(r)],
            "metadata": vars(r)
        }

    if flatten:
        return jsonlib.dumps(out_ramons.values()[0])

    return jsonlib.dumps(out_ramons)
Converts RAMON objects into a JSON string which can be directly written out
    to a .json file. You can pass either a single RAMON or a list. If you pass
    a single RAMON, it will still be exported with the ID as the key. In other
    words:

        type(from_json(to_json(ramon))) # ALWAYS returns a list

    ...even if `type(ramon)` is a RAMON, not a list.

    Arguments:
        ramons (RAMON or list): The RAMON object(s) to convert to JSON.
        flatten (bool : False): If ID should be used as a key. If not, then
            a single JSON document is returned.

    Returns:
        str: The JSON representation of the RAMON objects, in the schema:
            ```
            {
                <id>: {
                    type: . . . ,
                    metadata: {
                        . . .
                    }
                },
            }
            ```

    Raises:
        ValueError: If an invalid RAMON is passed.