From 0c4eecaad341945785430a3b04b85a60683cf974 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Thu, 29 Dec 2022 15:28:51 +0200 Subject: [PATCH] - Added example usage of serialization/deserialization to README - Changed targetType in dub.json to library --- README.md | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- dub.json | 5 ++-- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2872378..19c3c3a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,83 @@ libpb ## Example usage -TODO + +### Server initiation + +Firstly we create a new PocketBase instance to manage our server: + +```d +PocketBase pb = new PocketBase("http://127.0.0.1:8090/api/"); +``` + +### Serialization + +This is just to show off the serialization method `serializeRecord(RecordType)` which returns a `JSONValue` struct: + +```d + import std.algorithm.searching : canFind; + import std.string : cmp; + + struct Person + { + public string firstname, lastname; + public int age; + public string[] list; + public JSONValue extraJSON; + } + + Person p1; + p1.firstname = "Tristan"; + p1.lastname = "Kildaire"; + p1.age = 23; + p1.list = ["1", "2", "3"]; + p1.extraJSON = parseJSON(`{"item":1, "items":[1,2,3]}`); + + JSONValue serialized = PocketBase.serializeRecord(p1); + + string[] keys = serialized.object().keys(); + assert(canFind(keys, "firstname") && cmp(serialized["firstname"].str(), "Tristan") == 0); + assert(canFind(keys, "lastname") && cmp(serialized["lastname"].str(), "Kildaire") == 0); + assert(canFind(keys, "age") && serialized["age"].integer() == 23); +``` + +### Deserialization + +This is to show off deserialization method `fromJSON(RecordType)(JSONValue jsonIn)` which returns a struct of type `RecordType` (so far most features are implemented): + +```d + import std.string : cmp; + + struct Person + { + public string firstname, lastname; + public int age; + public bool isMale; + public JSONValue obj; + public int[] list; + } + + JSONValue json = parseJSON(`{ +"firstname" : "Tristan", +"lastname": "Kildaire", +"age": 23, +"obj" : {"bruh":1}, +"isMale": true, +"list": [1,2,3] +} +`); + + Person person = PocketBase.fromJSON!(Person)(json); + + writeln(person); + + assert(cmp(person.firstname, "Tristan") == 0); + assert(cmp(person.lastname, "Kildaire") == 0); + assert(person.age == 23); + assert(person.isMale == true); + //TODO: object test case, list test case +``` + ## License diff --git a/dub.json b/dub.json index 3ba2b6a..d7d538d 100644 --- a/dub.json +++ b/dub.json @@ -5,5 +5,6 @@ "copyright": "Copyright © 2022, Tristan B. Velloza Kildaire", "description": "PocketBase wrapper with serializer/deserializer support", "license": "LGPL v3.0", - "name": "libpb" -} \ No newline at end of file + "name": "libpb", + "targetType" : "library" +}