data.encoderhero #
hero Encoder
encoder hero is based on json2 from https://github.com/vlang/v/blob/master/vlib/x/json2/README.md
Usage
encode[T]
#!/usr/bin/env -S v -n -cg -w -enable-globals run
import freeflowuniverse.crystallib.data.encoderhero
import time
struct Person {
mut:
name string
age ?int = 20
birthday time.Time
deathday ?time.Time
}
mut person := Person{
name: 'Bob'
birthday: time.now()
}
person_json := encoderhero.encode[Person](person)!
decode[T]
import freeflowuniverse.crystallib.data.encoderhero
import time
struct Person {
mut:
name string
age ?int = 20
birthday time.Time
deathday ?time.Time
}
data := '
'
person := encoderhero.decode[Person](data)!
//
struct Person {
mut:
name "Bob"
age 20
birthday "2022-03-11 13:54:25"
}
License
for all original code as used from Alexander:
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file.
Constants #
const null = Null{}
fn decode #
fn decode[T](data string) !T
fn decode_array #
fn decode_array[T](_ []T, data string) ![]T
fn encode #
fn encode[T](val T) !string
encode is a generic function that encodes a type into a HEROSCRIPT string.
fn is_struct_array #
fn is_struct_array[U](_ []U) !bool
interface Encodable #
interface Encodable {
heroscript() string
}
Decodable is an interface, that allows custom implementations for encoding structs to their string based JSON representations
enum ValueKind #
enum ValueKind {
unknown
array
object
string_
number
}
ValueKind enumerates the kinds of possible values of the Any sumtype.
fn (ValueKind) str #
fn (k ValueKind) str() string
str returns the string representation of the specific ValueKind
struct Decoder #
struct Decoder[T] {
pub mut:
object T
data string
}
struct Encoder #
struct Encoder {
pub mut:
escape_unicode bool = true
action_name string
action_names []string
params paramsparser.Params
children []Encoder
parent ?&Encoder @[skip; str: skip]
}
Encoder encodes the an Any
type into HEROSCRIPT representation. It provides parameters in order to change the end result.
fn (Encoder) export #
fn (e Encoder) export() !string
export exports an encoder into encoded heroscript
fn (Encoder) add_child #
fn (mut e Encoder) add_child[T](val T, parent string) !
needs to be a struct we are adding parent is the name of the action e.g define.customer:contact
fn (Encoder) add_child_list #
fn (mut e Encoder) add_child_list[U](val []U, parent string) !
fn (Encoder) add #
fn (mut e Encoder) add[T](val T) !
needs to be a struct we are adding parent is the name of the action e.g define.customer:contact
fn (Encoder) encode_array #
fn (mut e Encoder) encode_array[U](val []U) !
fn (Encoder) encode_struct #
fn (mut e Encoder) encode_struct[T](t T) !
now encode the struct
struct Null #
struct Null {
is_null bool = true
}
Null
struct is a simple representation of the null
value in JSON.