From f5c4671bfbad96bf346bd7e9a21fc4317b4959df Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 3 Dec 2022 17:00:20 +0530 Subject: Adds most of the tools --- v_windows/v/old/vlib/x/json2/json2.v | 122 +++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 v_windows/v/old/vlib/x/json2/json2.v (limited to 'v_windows/v/old/vlib/x/json2/json2.v') diff --git a/v_windows/v/old/vlib/x/json2/json2.v b/v_windows/v/old/vlib/x/json2/json2.v new file mode 100644 index 0000000..0e5012c --- /dev/null +++ b/v_windows/v/old/vlib/x/json2/json2.v @@ -0,0 +1,122 @@ +// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. +module json2 + +pub const ( + null = Null{} +) + +pub interface Serializable { + from_json(f Any) + to_json() string +} + +// Decodes a JSON string into an `Any` type. Returns an option. +pub fn raw_decode(src string) ?Any { + mut p := new_parser(src, true) + return p.decode() +} + +// Same with `raw_decode`, but skips the type conversion for certain types when decoding a certain value. +pub fn fast_raw_decode(src string) ?Any { + mut p := new_parser(src, false) + return p.decode() +} + +// decode is a generic function that decodes a JSON string into the target type. +pub fn decode(src string) ?T { + res := raw_decode(src) ? + mut typ := T{} + typ.from_json(res) + return typ +} + +// encode is a generic function that encodes a type into a JSON string. +pub fn encode(typ T) string { + return typ.to_json() +} + +// as_map uses `Any` as a map. +pub fn (f Any) as_map() map[string]Any { + if f is map[string]Any { + return f + } else if f is []Any { + mut mp := map[string]Any{} + for i, fi in f { + mp['$i'] = fi + } + return mp + } + return map{ + '0': f + } +} + +// int uses `Any` as an integer. +pub fn (f Any) int() int { + match f { + int { return f } + i64, f32, f64, bool { return int(f) } + else { return 0 } + } +} + +// i64 uses `Any` as a 64-bit integer. +pub fn (f Any) i64() i64 { + match f { + i64 { return f } + int, f32, f64, bool { return i64(f) } + else { return 0 } + } +} + +// u64 uses `Any` as a 64-bit unsigned integer. +pub fn (f Any) u64() u64 { + match f { + u64 { return f } + int, i64, f32, f64, bool { return u64(f) } + else { return 0 } + } +} + +// f32 uses `Any` as a 32-bit float. +pub fn (f Any) f32() f32 { + match f { + f32 { return f } + int, i64, f64 { return f32(f) } + else { return 0.0 } + } +} + +// f64 uses `Any` as a float. +pub fn (f Any) f64() f64 { + match f { + f64 { return f } + int, i64, f32 { return f64(f) } + else { return 0.0 } + } +} + +// arr uses `Any` as an array. +pub fn (f Any) arr() []Any { + if f is []Any { + return f + } else if f is map[string]Any { + mut arr := []Any{} + for _, v in f { + arr << v + } + return arr + } + return [f] +} + +// bool uses `Any` as a bool +pub fn (f Any) bool() bool { + match f { + bool { return f } + string { return f.bool() } + else { return false } + } +} -- cgit v1.2.3