# Encoding

The functions for converting data from one format to another are described here.

* [Json( obj o ) str](/stdlib/encoding.md#json-obj-o-str)
* [JsonToObj( str s ) obj](/stdlib/encoding.md#jsontoobj-str-s-obj)
* [StructDecode( b buf, struct s )](/stdlib/encoding.md#structdecode-buf-b-struct-s)
* [StructEncode( struct s ) buf](/stdlib/encoding.md#structencode-struct-s-buf)

## Functions

### Json( obj o ) str

The *Json* function converts a variable of *obj* type into **json** string.

### JsonToObj( str s ) obj

The *JsonToObj* function converts **json** string into a variable of the *obj* type.

```go
run str {
  return Json(JsonToObj(`{
         "int": 1234,
         "str": "value",
         "float": -45.67,
          "list":[{"on": true},
            "sub 2",
            "sub 3",
            {
                "q": "OK"
            }]
    }`))
}
// Result {"float":-45.67,"int":1234,"list":[{"on":true},"sub 2","sub 3",{"q":"OK"}],"str":"value"}
```

### StructDecode( buf b, struct s )

The *StructDecode* function converts the binary data of a *buf* variable to the field values of the specified structure variable. The binary data must be created with *StructEncode* function.

```go
  time t
  StructDecode(StructEncode(Now()), t)
```

### StructEncode( struct s ) buf

The *StructEncode* function converts a structure variable to binary and stores the result into a *buf* variable. The function saves only fields of the following types: **int, bool, char, float, buf, str**. Fields of other types are skipped.

```go
struct tmp {
    str head
    int i
}

run str {
  tmp t1 = {head: `HEADER`, i: -356}
  buf bout = StructEncode(t1)
  ...
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gentee.org/stdlib/encoding.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
