Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/get_weather/get_weather.v
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import net.http
import os
import rand
import x.json2
import x.json2.decoder2 as json
import x.json2 as json

struct Weather {
lang string
Expand Down Expand Up @@ -53,7 +52,7 @@ fn translate(q string, sl string, tl string) !string {

resp := http.fetch(http.FetchConfig{ ...config, url: url })!

json_resp := json.decode[json2.Any](resp.body)!
json_resp := json.decode[json.Any](resp.body)!

a := json_resp.arr()
if a.len > 0 {
Expand Down
6 changes: 3 additions & 3 deletions examples/news_fetcher.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import net.http
import x.json2.decoder2
import x.json2
import sync.pool

struct Story {
Expand All @@ -16,7 +16,7 @@ fn worker_fetch(mut p pool.PoolProcessor, cursor int, worker_id int) voidptr {
println('failed to fetch data from /v0/item/${id}.json')
return pool.no_result
}
story := decoder2.decode[Story](resp.body) or {
story := json2.decode[Story](resp.body) or {
println('failed to decode a story')
return pool.no_result
}
Expand All @@ -31,7 +31,7 @@ fn main() {
return
}

ids := decoder2.decode[[]int](resp.body) or {
ids := json2.decode[[]int](resp.body) or {
println('failed to decode topstories.json ${err}')
return
}#[0..30]
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/tests/comptime_selector_assign.vv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import x.json2 { Any, raw_decode }
import x.json2 { Any, decode }

struct Income {
mut:
Expand All @@ -22,7 +22,7 @@ pub fn structuring[T](res Any) T {
}

fn main() {
res := raw_decode('{
res := decode[Any]('{
"email": ["sdvsdv", "sds"],
"code": 12
}')!.as_map()
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/aliases/alias_sumtype_method_call_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct Data {

fn test_alias_sumtype_method_call() {
a := '{"a":"a","b":1}'
json := json2.raw_decode(a) or { panic(err) }
json := json2.decode[json2.Any](a) or { panic(err) }
data := Data{json}
json_str := data.prop.str()
println(json_str)
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/comptime/comptime_map_generic_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mut:

fn test_main() {
user_json := '{"numbers":{"home":"123456","work":"987653"}}'
res := json2.raw_decode(user_json)!.as_map()
res := json2.decode[json2.Any](user_json)!.as_map()

mut numbers := map[string]string{}
decode_map(mut numbers, res['numbers']!.as_map())!
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/result_with_index_expr_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn test_result_with_index() {
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipitsuscipit recusandae consequuntur expedita et cumreprehenderit molestiae ut ut quas totamnostrum rerum est autem sunt rem eveniet architecto"
}'
raw_data := json2.raw_decode(resp)!
raw_data := json2.decode[json2.Any](resp)!

data := raw_data as map[string]json2.Any

Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/skip_unused/generic_call_from_json.vv
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct App {

@['/'; POST]
fn (app &App) index(mut ctx Context) veb.Result {
j := json2.raw_decode(ctx.req.data) or { return ctx.request_error('error') }
j := json2.decode[json2.Any](ctx.req.data) or { return ctx.request_error('error') }

m := j.as_map()
myarr := m['myarr'] or { panic(error) }
Expand Down
15 changes: 7 additions & 8 deletions vlib/x/json2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ mut:
fn main() {
resp := '{"name": "Bob", "age": 20, "birthday": "${time.now()}"}'
person := json2.decode[Person](resp)!
/*
struct Person {
mut:
name "Bob"
age 20
birthday "2022-03-11 13:54:25"
}
*/
// struct Person {
// mut:
// name "Bob"
// age 20
// birthday "2022-03-11 13:54:25"
// deathday "2022-03-11 13:54:25"
// }
}
```

Expand Down
2 changes: 1 addition & 1 deletion vlib/x/json2/decoder2/check.v → vlib/x/json2/check.v
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module decoder2
module json2

// increment checks eof and increments checker by one
@[inline]
Expand Down
49 changes: 49 additions & 0 deletions vlib/x/json2/custom.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,52 @@ pub interface JsonEncoder {
pub interface Encodable {
json_str() string
}

// implements decoding json strings, e.g. "hello, \u2164!"
pub interface StringDecoder {
mut:
// called with raw string (minus apostrophes) e.g. 'hello, \u2164!'
from_json_string(raw_string string) !
}

// implements decoding json numbers, e.g. -1.234e23
pub interface NumberDecoder {
mut:
// called with raw string of number e.g. '-1.234e23'
from_json_number(raw_number string) !
}

// implements decoding json true/false
pub interface BooleanDecoder {
mut:
// called with converted bool
// already checked so no error needed
from_json_boolean(boolean_value bool)
}

// implements decoding json null
pub interface NullDecoder {
mut:
// only has one value
// already checked so no error needed
from_json_null()
}

// Implement once generic interfaces are more stable

// // implements decoding json arrays, e.g. ["hi", "bye", 0.9, true]
// // elements are already decoded
// pub interface ArrayDecoder[T] {
// mut:
// // called for every element in array e.g. 'hi', 'bye', '0.9', 'true'
// from_json_value(raw_value T)!
// }

// // implements decoding json object, e.g. {"name": "foo", "name": "bar", "age": 99}
// // this allows for duplicate/ordered keys to be decoded
// // elements are already decoded
// pub interface ObjectDecoder[T] {
// mut:
// // called for every key-value pair in object (minus apostrophes for keys) e.g. ('name': 'foo'), ('name': 'bar'), ('age': '99')
// from_json_pair(raw_key string, raw_value T)!
// }
12 changes: 1 addition & 11 deletions vlib/x/json2/decoder2/decode.v → vlib/x/json2/decode.v
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module decoder2
module json2

import strconv
import strings
Expand Down Expand Up @@ -122,16 +122,6 @@ fn (list &LinkedList[T]) free() {
list.len = 0
}

// ValueKind represents the kind of a JSON value.
enum ValueKind {
array
object
string
number
boolean
null
}

const max_context_length = 50
const max_extra_characters = 5
const tab_width = 8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module decoder2
module json2

import time

Expand Down
Loading
Loading