Thoughts on handling string type as JSON?
Closed this issue ยท 6 comments
Proposal
The current API internally serializes instances of type T
to JSON.
private static string CreateJsonNewlines<T>(IEnumerable<T> documents, JsonSerializerOptions jsonOptions)
=> String.Join('\n', documents.Select(x => JsonSerializer.Serialize(x, jsonOptions)));
If the type parameter T
is string, bypass the serialization and assume that the incoming string has already been serialized.
Reasoning
This would allow for more generic insertions using the API in cases where the document has already been generated/serialized via some other mechanism or is being generated as JSON in another part of the stack and is being batched for submission.
Would love to get some thoughts and put together a PR if this makes sense.
Hi @CharlieDigital, thanks for opening the proposal.
It's currently holiday in my country, so I'll give a comprehensive response next week.
// Rune
Hi @CharlieDigital,
I decided that it would be better to implement it as an overloaded method.
Here is an example of the signatures for CreateDocument<T>
Task<T> CreateDocument<T>(string collection, T document) where T : class;
Task<T> CreateDocument<T>(string collection, string document) where T : class;
I think it would be easiest if I implement the change, since there are some things I need to restructure in the code-base to make it work. I'll let you know in this PR when it has been implemented.
// Rune
No objections ๐ but happy to help if I can.
@CharlieDigital this has now been fixed in #115
All create and update methods now supports taking in documents that has already been JSON serialized. In the specific example you requested, there are now 3 implementations. Here are the two new overloads.
The first one takes in a string
that has already been formatted in the JSON-Newline format.
Task<List<ImportResponse>> ImportDocuments<T>(string collection, string documents, int batchSize = 40, ImportType importType = ImportType.Create);
The second implementation takes in a collection
of documents that has already been serialized as JSON. This implementation I'll handle the JSON-Newline conversion for you.
Task<List<ImportResponse>> ImportDocuments<T>(string collection, IEnumerable<string> documents, int batchSize = 40, ImportType importType = ImportType.Create);
Before I make a new release, let me know if this solves the missing features you've requested.
This looks good to go and I think the flexibility of using either a string or enumerable is great!
@CharlieDigital I've made a new release, 5.1.0.
Thanks for the feature request. ๐