def device_measurement(device,
                       ts=None,
                       part=None,
                       result=None,
                       code=None,
                       **kwargs):
    """Returns a JSON MeasurementPayload ready to be send through a
    transport.

    If `ts` is not given, the current time is used. `part` is an
    optional `Part` object, and `result` and `code` are the respective
    fields of the `Measurement` object. All other arguments are
    interpreted as dimensions.

    Minimal example, using a `Device` object to send two
    measurements:

    >>> d = Device("12345")
    >>> def publish(msg):
    ...     pass
    >>> publish(d.measurement(temperature=22.8))
    >>> publish(d.measurement(pressure=4.1))

    """
    if ts is None:
        ts = local_now()

    payload = MeasurementPayload(device=device, part=part)
    m = Measurement(ts, result, code, list(kwargs))
    payload.measurements.append(m)
    m.add_sample(ts, **kwargs)
    return dumps(payload)