|
1 | | -defmodule GRPC.Codec.JSON do |
2 | | - @moduledoc """ |
3 | | - JSON Codec for gRPC communication. |
| 1 | +if Code.ensure_loaded?(Jason) do |
| 2 | + defmodule GRPC.Codec.JSON do |
| 3 | + @moduledoc """ |
| 4 | + JSON Codec for gRPC communication. |
4 | 5 |
|
5 | | - This module implements the `GRPC.Codec` behaviour, providing encoding and decoding functions |
6 | | - for JSON serialization in the context of gRPC communication. |
| 6 | + This module implements the `GRPC.Codec` behaviour, providing encoding and decoding functions |
| 7 | + for JSON serialization in the context of gRPC communication. |
7 | 8 |
|
8 | | - ## Behavior Functions |
| 9 | + ## Behavior Functions |
9 | 10 |
|
10 | | - - `name/0`: Returns the name of the codec, which is "json". |
11 | | - - `encode/1`: Encodes a struct using the Protobuf.JSON.encode!/1 function. |
12 | | - - `decode/2`: Decodes binary data into a map using the Jason library. |
| 11 | + - `name/0`: Returns the name of the codec, which is "json". |
| 12 | + - `encode/1`: Encodes a struct using the Protobuf.JSON.encode!/1 function. |
| 13 | + - `decode/2`: Decodes binary data into a map using the Jason library. |
13 | 14 |
|
14 | | - This module requires the Jason dependency. |
15 | | - """ |
16 | | - @behaviour GRPC.Codec |
| 15 | + This module requires the Jason dependency. |
| 16 | + """ |
| 17 | + @behaviour GRPC.Codec |
17 | 18 |
|
18 | | - def name(), do: "json" |
| 19 | + def name(), do: "json" |
19 | 20 |
|
20 | | - @doc """ |
21 | | - Encodes a struct using the Protobuf.JSON.encode!/1 function. |
| 21 | + @doc """ |
| 22 | + Encodes a struct using the Protobuf.JSON.encode!/1 function. |
22 | 23 |
|
23 | | - ### Parameters: |
| 24 | + ### Parameters: |
24 | 25 |
|
25 | | - - `struct` - The struct to be encoded. |
| 26 | + - `struct` - The struct to be encoded. |
26 | 27 |
|
27 | | - ### Returns: |
| 28 | + ### Returns: |
28 | 29 |
|
29 | | - The encoded binary data. |
| 30 | + The encoded binary data. |
30 | 31 |
|
31 | | - ### Example: |
| 32 | + ### Example: |
32 | 33 |
|
33 | | - ```elixir |
34 | | - %MyStruct{id: 1, name: "John"} |> GRPC.Codec.JSON.encode() |
35 | | - ``` |
| 34 | + ```elixir |
| 35 | + %MyStruct{id: 1, name: "John"} |> GRPC.Codec.JSON.encode() |
| 36 | + ``` |
36 | 37 |
|
37 | | - """ |
| 38 | + """ |
38 | 39 |
|
39 | | - def encode(struct) do |
40 | | - Protobuf.JSON.encode!(struct) |
41 | | - end |
| 40 | + def encode(struct) do |
| 41 | + Protobuf.JSON.encode!(struct) |
| 42 | + end |
42 | 43 |
|
43 | | - @doc """ |
44 | | - Decodes binary data into a map using the Jason library. |
45 | | - Parameters: |
| 44 | + @doc """ |
| 45 | + Decodes binary data into a map using the Jason library. |
| 46 | + Parameters: |
46 | 47 |
|
47 | | - binary - The binary data to be decoded. |
48 | | - module - Module to be created. |
| 48 | + binary - The binary data to be decoded. |
| 49 | + module - Module to be created. |
49 | 50 |
|
50 | | - Returns: |
| 51 | + Returns: |
51 | 52 |
|
52 | | - A map representing the decoded data. |
| 53 | + A map representing the decoded data. |
53 | 54 |
|
54 | | - Raises: |
| 55 | + Raises: |
55 | 56 |
|
56 | | - Raises an error if the Jason library is not loaded. |
| 57 | + Raises an error if the Jason library is not loaded. |
57 | 58 |
|
58 | | - Example: |
| 59 | + Example: |
59 | 60 |
|
60 | | - ```elixir |
61 | | - binary_data |> GRPC.Codec.JSON.decode(__MODULE__) |
62 | | - ``` |
63 | | - """ |
64 | | - def decode(<<>>, _module), do: %{} |
| 61 | + ```elixir |
| 62 | + binary_data |> GRPC.Codec.JSON.decode(__MODULE__) |
| 63 | + ``` |
| 64 | + """ |
| 65 | + def decode(<<>>, _module), do: %{} |
65 | 66 |
|
66 | | - def decode(binary, _module), do: Jason.decode!(binary) |
| 67 | + def decode(binary, _module), do: Jason.decode!(binary) |
| 68 | + end |
| 69 | +else |
| 70 | + defmodule GRPC.Codec.JSON do |
| 71 | + def decode(_, _), do: raise(ArgumentError, "Module Jason not found") |
| 72 | + end |
67 | 73 | end |
0 commit comments