Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * json_test.c |
| 4 | * |
| 5 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | #include <vppinfra/clib.h> |
| 20 | #include <vppinfra/vec.h> |
| 21 | #include <vat/vat.h> |
| 22 | #include <vat/json_format.h> |
| 23 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 24 | static void |
| 25 | print_json_test (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 27 | vat_json_node_t node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 29 | vat_json_init_object (&node); |
| 30 | vat_json_object_add_string_copy (&node, "str", (u8 *) "string-value"); |
| 31 | vat_json_object_add_uint (&node, "ab", 127); |
| 32 | vat_json_object_add_real (&node, "pi", 3.14159f); |
| 33 | vat_json_print (stdout, &node); |
| 34 | vat_json_free (&node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 36 | vat_json_init_object (&node); |
| 37 | vat_json_node_t *a1 = vat_json_object_add (&node, "a1"); |
| 38 | vat_json_init_object (a1); |
| 39 | vat_json_object_add_uint (a1, "b1", 512); |
| 40 | vat_json_object_add_string_copy (a1, "b2", (u8 *) "string"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 42 | vat_json_object_add_int (&node, "a2", 2); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 44 | vat_json_node_t *a3 = vat_json_object_add_list (&node, "a3"); |
| 45 | vat_json_init_array (a3); |
| 46 | vat_json_array_add_uint (a3, 1); |
| 47 | vat_json_array_add_int (a3, -2); |
| 48 | vat_json_array_add_uint (a3, 3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 50 | vat_json_init_object (vat_json_object_add (&node, "a4")); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 52 | struct in_addr ipv4 = { 0 }; |
| 53 | struct in6_addr ipv6 = { {{0}} }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 55 | vat_json_object_add_ip4 (&node, "ipv4", ipv4); |
| 56 | vat_json_object_add_ip6 (&node, "ipv6", ipv6); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 58 | vat_json_print (stdout, &node); |
| 59 | vat_json_free (&node); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 62 | int |
| 63 | main (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 65 | print_json_test (); |
| 66 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | } |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * fd.io coding-style-patch-verification: ON |
| 71 | * |
| 72 | * Local Variables: |
| 73 | * eval: (c-set-style "gnu") |
| 74 | * End: |
| 75 | */ |