populateFromJSON

Undocumented in source. Be warned that the author may not have intended to support it.
  1. void populateFromJSON(T object, JSON data)
  2. void populateFromJSON(T object, JSON data, string pathPrefix)
    void
    populateFromJSON
    (
    T
    )
    (
    ref T object
    ,,
    string pathPrefix = ""
    )

Examples

populateFromJSON - ensure that deserialization works with valid JSON data

import dutils.validation.constraints : ValidateMinimumLength,
  ValidateMaximumLength, ValidateMinimum, ValidateEmail, ValidateRequired;

struct Person {
  @ValidateMinimumLength(2)
  @ValidateMaximumLength(100)
  string name;

  @ValidateMinimum!double(20) double height;

  @ValidateEmail()
  @ValidateRequired()
  string email;

  @ValidateRequired()
  bool member;
}

auto data = JSON([
    "does not exists": JSON(true),
    "name": JSON("Anna"),
    "height": JSON(170.1),
    "email": JSON("anna@example.com"),
    "member": JSON(true)
    ]);

Person person;
populateFromJSON(person, data);

assert(person.name == "Anna", "expected name Anna");
assert(person.height == 170.1, "expected height 170.1");
assert(person.email == "anna@example.com", "expected email anna@example.com");
assert(person.member == true, "expected member true");

populateFromJSON - ensure that validation errors are thrown with invalid JSON data

import std.conv : to;
import dutils.validation.validate : ValidationError, ValidationErrors;
import dutils.validation.constraints : ValidationErrorTypes, ValidateMinimumLength,
  ValidateMaximumLength, ValidateMinimum, ValidateEmail, ValidateRequired;

struct Person {
  @ValidateMinimumLength(2)
  @ValidateMaximumLength(100)
  string name;

  @ValidateMinimum!float(20) float height;

  @ValidateEmail()
  @ValidateRequired()
  string email;

  @ValidateRequired()
  bool member;
}

auto data = JSON([
    "does not exists": JSON(true),
    "name": JSON("Anna"),
    "height": JSON("not a number")
    ]);

Person person;

auto catched = false;
try {
  populateFromJSON(person, data);
} catch (ValidationErrors validation) {
  catched = true;
  assert(validation.errors.length == 4,
      "expected 4 errors, got " ~ validation.errors.length.to!string
      ~ " with message: " ~ validation.msg);
  assert(validation.errors[0].type == "type", "expected minimumLength error");
  assert(validation.errors[0].path == "height", "expected error path to be height");
  assert(validation.errors[1].type == "minimum", "expected minimum error");
  assert(validation.errors[1].path == "height", "expected error path to be height");
  assert(validation.errors[2].type == "required", "expected required error");
  assert(validation.errors[2].path == "email", "expected error path to be email");
  assert(validation.errors[3].type == "required", "expected required error");
  assert(validation.errors[3].path == "member", "expected error path to be member");
}

assert(catched == true, "did not catch the expected errors");

populateFromJSON - should populate

import dutils.validation.constraints : ValidateRequired, ValidateEmail;

struct Email {
  @ValidateRequired()
  @ValidateEmail()
  string to;

  @ValidateEmail()
  string from;

  string subject;

  @ValidateRequired()
  string body;
}

auto data = JSON([
    "does not exists": JSON(true),
    "to": JSON("anna@example.com"),
    "body": JSON("Some text")
    ]);

Email email;
populateFromJSON(email, data);

assert(email.to == "anna@example.com", "expected to to be anna@example.com");
assert(email.from == "", "expected from to be \"\"");
assert(email.subject == "", "expected from to be \"\"");
assert(email.body == "Some text", "expected from to be \"Some text\"");

Meta