Skip to content

Commit

Permalink
Merge pull request #59 from v-community/add-json-example-for-V
Browse files Browse the repository at this point in the history
Add JSON example
  • Loading branch information
vbrazo committed Nov 4, 2019
2 parents f478be7 + c8969d8 commit 29ff57a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -5,7 +5,7 @@
*.tmp.c
a.out.tmp.c
*.vrepl.v
*.vrepl
*.vrepl*
.vrepl_temp

.DS_Store
Expand Down
25 changes: 25 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,22 @@
# Changelog

## v0.6.0 (TBD)

## Documentation

- [PR #58](https://github.com/v-community/v_by_example/pull/58) add coma and remove some whitespaces [@donnisnoni95](https://github.com/donnisnoni95)
- [PR #57](https://github.com/v-community/v_by_example/pull/57) array: add how to create an empty array [@donnisnoni95](https://github.com/donnisnoni95)
- [PR #41](https://github.com/v-community/v_by_example/pull/41) examples: improve grammar, better MD structure & fix some code [@donnisnoni95](https://github.com/donnisnoni95)
- [PR #40](https://github.com/v-community/v_by_example/pull/40) November and December plan [@vbrazo](https://github.com/vbrazo)
- [PR #38](https://github.com/v-community/v_by_example/pull/38) Improve contributing [@vbrazo](https://github.com/vbrazo)
- [PR #37](https://github.com/v-community/v_by_example/pull/37) Fix broken README links [@vbrazo](https://github.com/vbrazo)

## Feature request

- [PR #59](https://github.com/v-community/v_by_example/pull/59) Add JSON example [@vbrazo](https://github.com/vbrazo)

------------------------------------------------------------------------------

## v0.5.0 (2019-10-24)

## Bug
Expand All @@ -17,6 +34,8 @@

- [PR #26](https://github.com/v-community/v_by_example/pull/26) Follow #25 and more progress [@Delta456](https://github.com/Delta456)

------------------------------------------------------------------------------

## v0.4.0 (2019-10-13)

## Chores
Expand All @@ -27,6 +46,8 @@

- [PR #20](https://github.com/v-community/v_by_example/pull/20) example: functions, arrays, structures etc [@vbrazo](https://github.com/vbrazo)

------------------------------------------------------------------------------

## v0.3.0 (2019-10-12)

## Chores
Expand All @@ -42,6 +63,8 @@

- [PR #19](https://github.com/v-community/v_by_example/pull/19) example: conditional statements like if, else, else if, for and while [@vbrazo](https://github.com/vbrazo)

------------------------------------------------------------------------------

## v0.2.0 (2019-10-06)

## Feature Request / Enhancement
Expand All @@ -60,6 +83,8 @@

- [PR #12](https://github.com/v-community/v_by_example/pull/12) ignore executable chores [@donnisnoni95](https://github.com/donnisnoni95)

------------------------------------------------------------------------------

## v0.1.0 (2019-09-26)

## Feature Request / Enhancement
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -16,6 +16,7 @@ Discord server: https://discord.gg/d3Qk65J
- [Conditional Statements](examples/conditional_statements/conditional_statements.md)
- [Loops](examples/loops/loops.md)
- [Functions](examples/functions/functions.md)
- [JSON](examples/json.md)
- [Keywords](examples/keywords.md)
- [Operator](examples/operator.md)
- [Primitives](examples/primitives/primitives.md)
Expand Down
66 changes: 66 additions & 0 deletions examples/json.md
@@ -0,0 +1,66 @@
## JSON

JavaScript Object Notation (JSON) is a lightweight data-interchange format that is easy for humans to read and write. Furthermore, equally simple for machines to generate and/or parse. JSON is completely language agnostic and that's why it's the ideal interchange format.

To read more about JSON visit: [json.org](http://json.org).

## Parsing JSON

To parse a JSON string received by another application or generated within your existing application:

```go
import json

struct Customer {
first_name string
last_name string
hometown string
}

fn main() {
customers_string := '[{ "first_name": "Vitor", "last_name": "Oliveira", "hometown": "Rio de Janeiro" }, { "first_name": "Don", "last_name": "Nisnoni", "hometown": "Kupang" }]'
customers := json.decode([]Customer, customers_string) or {
eprintln('Failed to parse json')
return
}

// Print the list of customers
for customer in customers {
println('$customer.first_name $customer.last_name: $customer.hometown')
}
}
```

## Generating JSON

Creating a JSON string for communication or serialization is just as simple. We decode and encode in the example below:

```go
import json

struct Customer {
first_name string
last_name string
hometown string
}

fn main() {
customer_string := '[{ "first_name": "Vitor", "last_name": "Oliveira", "hometown": "Rio de Janeiro"}]'

customer := json.decode([]Customer, customer_string) or {
eprintln('Failed to parse json')
return
}

encoded_json := json.encode(customer)

println(encoded_json)
}
```

## Exercises

1. Compare how you handle JSON in your favorite language and V.
2. Build an `Address` struct that contains address information.
3. Use the `Address` struct to decode and encode a string that contains JSON format.
4. Create 2 structs: `Address` and `User` where a user has many addresses. Now receive a string with a nested JSON like `'[{ "first_name": "Vitor", "last_name": "Oliveira", "hometown": "Rio de Janeiro", "addresses": [{ street_name: "Rua Princesa Isabel", city: "Rio de Janeiro", country: "Brazil" }] }]'`, decode and encode it.

0 comments on commit 29ff57a

Please sign in to comment.