Skip to content

Add msgspec.json.Encoder.encode_lines - #479

Merged
jcrist merged 1 commit into
mainfrom
json-encode-lines
Jul 11, 2023
Merged

jcrist merged 1 commit into
mainfrom
json-encode-lines

Conversation

@jcrist

@jcrist jcrist commented Jul 11, 2023

Copy link
Copy Markdown
Member

This adds a new method on the JSON Encoder class for encoding an iterable of items as newline delimited JSON, one item per line. This is roughly equivalent to (but up to 3x faster than):

def encode_lines(items):
    return b''.join(
        msgspec.json.encode(item) + b'\n' for item in items
    )

We limit it to an implementation on the Encoder class alone for now (rather than a new top-level function) since this is a relatively niche feature.

Fixes #449.

This adds a new method on the JSON `Encoder` class for encoding an
iterable of items as newline delimited JSON, one item per line. This is
roughly equivalent to (but up to 3x faster than):

```
def encode_lines(items):
    return b''.join(
        msgspec.json.encode(item) + b'\n' for item in items
    )
```

We limit it to an implementation on the `Encoder` class alone for now
(rather than a new top-level function) since this is a relatively niche
feature.
@jcrist
jcrist merged commit 473d6d3 into main Jul 11, 2023
@jcrist
jcrist deleted the json-encode-lines branch July 11, 2023 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for JSON lines (JSON line-delimited)

1 participant