github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/infra/conf/json/yaml_test.go (about)

     1  package json_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	. "github.com/v2fly/v2ray-core/v5/infra/conf/json"
     8  )
     9  
    10  func TestYMLToJSON_V2Style(t *testing.T) {
    11  	input := `
    12  log:
    13    loglevel: debug
    14  inbounds:
    15  - port: 10800
    16    listen: 127.0.0.1
    17    protocol: socks
    18    settings:
    19      udp: true
    20  outbounds:
    21  - protocol: vmess
    22    settings:
    23      vnext:
    24      - address: example.com
    25        port: 443
    26        users:
    27        - id: '98a15fa6-2eb1-edd5-50ea-cfc428aaab78'
    28    streamSettings:
    29      network: tcp
    30      security: tls
    31  `
    32  	expected := `
    33  {
    34      "log": {
    35          "loglevel": "debug"
    36      },
    37      "inbounds": [{
    38          "port": 10800,
    39          "listen": "127.0.0.1",
    40          "protocol": "socks",
    41          "settings": {
    42              "udp": true
    43          }
    44      }],
    45      "outbounds": [{
    46          "protocol": "vmess",
    47          "settings": {
    48              "vnext": [{
    49                  "port": 443,
    50                  "address": "example.com",
    51                  "users": [{
    52                      "id": "98a15fa6-2eb1-edd5-50ea-cfc428aaab78"
    53                  }]
    54              }]
    55          },
    56          "streamSettings": {
    57              "network": "tcp",
    58              "security": "tls"
    59          }
    60      }]
    61  }
    62  `
    63  	bs, err := FromYAML([]byte(input))
    64  	if err != nil {
    65  		t.Error(err)
    66  	}
    67  	m := make(map[string]interface{})
    68  	json.Unmarshal(bs, &m)
    69  	assertResult(t, m, expected)
    70  }
    71  
    72  func TestYMLToJSON_ValueTypes(t *testing.T) {
    73  	input := `
    74  boolean: 
    75      - TRUE
    76      - FALSE
    77      - true
    78      - false
    79  float:
    80      - 3.14
    81      - 6.8523015e+5
    82  int:
    83      - 123
    84      - 0b1010_0111_0100_1010_1110
    85  null:
    86      nodeName: 'node'
    87      parent: ~  # ~ for null
    88  string:
    89      - 哈哈
    90      - 'Hello world'
    91      - newline
    92        newline2    # multi-line string
    93  date:
    94      - 2018-02-17    # yyyy-MM-dd
    95  datetime: 
    96      -  2018-02-17T15:02:31+08:00    # ISO 8601 time
    97  mixed:
    98      - true
    99      - false
   100      - 1
   101      - 0
   102      - null
   103      - hello
   104  # arbitrary keys
   105  1: 0
   106  true: false
   107  TRUE: TRUE
   108  "str": "hello"
   109  `
   110  	expected := `
   111  {
   112      "boolean": [true, false, true, false],
   113      "float": [3.14, 685230.15],
   114      "int": [123, 685230],
   115      "null": {
   116          "nodeName": "node",
   117          "parent": null
   118      },
   119      "string": ["哈哈", "Hello world",  "newline newline2"],
   120      "date": ["2018-02-17T00:00:00Z"],
   121      "datetime": ["2018-02-17T15:02:31+08:00"],
   122      "mixed": [true,false,1,0,null,"hello"],
   123      "1": 0,
   124      "true": true,
   125      "str": "hello"
   126  }
   127  `
   128  	bs, err := FromYAML([]byte(input))
   129  	if err != nil {
   130  		t.Error(err)
   131  	}
   132  	m := make(map[string]interface{})
   133  	json.Unmarshal(bs, &m)
   134  	assertResult(t, m, expected)
   135  }