www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - vibe.d json deserializeJson!Foo and ddbc.update!Foo

reply Chris Bare <chris bareflix.com> writes:
I'm working on a project with vibe.d and ddbc to access my 
database.
Both have powerful functions that operate on Struct types to 
avoid a lot of boilerplate code.
I have a web site that sends a json message to update a record.
deserializeJson creates a new Foo record containing the json data.
However, there are some fields in the record which are not in the 
json, and these get default values.
I have another Foo that has the original values.

What would be the best way to merge the updated values into the 
original?
Obviously I can copy from one Foo to the other, but that's pretty 
inelegant.
If I could pass an existing Foo to deserializeJson and have only 
some fields filled in, that would be perfect.
I tried this:

deserializeJson!Foo(foo, req.json);

but it completely overwrites all the fields in foo.

Any suggestions? Is there a trick I'm just not seeing?

--
Chris
Feb 12 2021
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 2/12/21 6:22 PM, Chris Bare wrote:
 I'm working on a project with vibe.d and ddbc to access my database.
 Both have powerful functions that operate on Struct types to avoid a lot 
 of boilerplate code.
 I have a web site that sends a json message to update a record.
 deserializeJson creates a new Foo record containing the json data.
 However, there are some fields in the record which are not in the json, 
 and these get default values.
 I have another Foo that has the original values.
 
 What would be the best way to merge the updated values into the original?
 Obviously I can copy from one Foo to the other, but that's pretty 
 inelegant.
 If I could pass an existing Foo to deserializeJson and have only some 
 fields filled in, that would be perfect.
 I tried this:
 
 deserializeJson!Foo(foo, req.json);
 
 but it completely overwrites all the fields in foo.
 
 Any suggestions? Is there a trick I'm just not seeing?
Does ignore work? That's a UDA that tells vibe to ignore the field. Though I don't know if it means it leaves it alone completely. https://vibed.org/api/vibe.data.serialization/ignore -Steve
Feb 12 2021
parent Chris Bare <chris bareflix.com> writes:
On Saturday, 13 February 2021 at 01:21:56 UTC, Steven 
Schveighoffer wrote:
 On 2/12/21 6:22 PM, Chris Bare wrote:
 [...]
Does ignore work? That's a UDA that tells vibe to ignore the field. Though I don't know if it means it leaves it alone completely. https://vibed.org/api/vibe.data.serialization/ignore -Steve
ignore just prevents it from throwing an error if the field is not in the json. The field still gets overwritten with it's default value in the struct.
Feb 13 2021