Fix issues with json files

File parsing no longer uses istream, which strips whitespace. Moved hard-coded json to its own file.

Signed-off-by: Alexander Yee <xander.yee@gmail.com>
Change-Id: I342b74fd1cc8c5af7721af5955aa7d7b185ac71e
diff --git a/aos/BUILD b/aos/BUILD
index b8468bd..0792e67 100644
--- a/aos/BUILD
+++ b/aos/BUILD
@@ -411,9 +411,11 @@
     ],
     data = [
         ":json_to_flatbuffer_fbs_reflection_out",
+        ":json_to_flatbuffer_test_spaces.json",
     ],
     target_compatible_with = ["@platforms//os:linux"],
     deps = [
+        ":flatbuffer_merge",
         ":json_to_flatbuffer",
         ":json_to_flatbuffer_fbs",
         "//aos/testing:googletest",
diff --git a/aos/json_to_flatbuffer.h b/aos/json_to_flatbuffer.h
index d5e1039..6eab22a 100644
--- a/aos/json_to_flatbuffer.h
+++ b/aos/json_to_flatbuffer.h
@@ -109,10 +109,8 @@
 template <typename T>
 inline FlatbufferDetachedBuffer<T> JsonFileToFlatbuffer(
     const std::string_view path) {
-  std::ifstream t{std::string(path)};
-  std::istream_iterator<char> start(t), end;
-  std::string result(start, end);
-  return FlatbufferDetachedBuffer<T>(JsonToFlatbuffer<T>(result));
+  return FlatbufferDetachedBuffer<T>(
+      JsonToFlatbuffer<T>(util::ReadFileToStringOrDie(path)));
 }
 
 // Parses a file as a binary flatbuffer or dies.
diff --git a/aos/json_to_flatbuffer_test.cc b/aos/json_to_flatbuffer_test.cc
index 6772826..fddb431 100644
--- a/aos/json_to_flatbuffer_test.cc
+++ b/aos/json_to_flatbuffer_test.cc
@@ -1,5 +1,6 @@
 #include "aos/json_to_flatbuffer.h"
 
+#include "aos/flatbuffer_merge.h"
 #include "aos/json_to_flatbuffer_generated.h"
 #include "aos/testing/path.h"
 #include "flatbuffers/minireflect.h"
@@ -301,5 +302,20 @@
                                           ConfigurationTypeTable()));
 }
 
+TEST_F(JsonToFlatbufferTest, SpacedData) {
+  EXPECT_TRUE(CompareFlatBuffer(
+      FlatbufferDetachedBuffer<VectorOfStrings>(
+          JsonToFlatbuffer<VectorOfStrings>(R"json({
+	"str": [
+		"f o o",
+		"b a r",
+		"foo bar",
+		"bar foo"
+	]
+})json")),
+      JsonFileToFlatbuffer<VectorOfStrings>(
+          ArtifactPath("aos/json_to_flatbuffer_test_spaces.json"))));
+}
+
 }  // namespace testing
 }  // namespace aos
diff --git a/aos/json_to_flatbuffer_test_spaces.json b/aos/json_to_flatbuffer_test_spaces.json
new file mode 100644
index 0000000..c864037
--- /dev/null
+++ b/aos/json_to_flatbuffer_test_spaces.json
@@ -0,0 +1,8 @@
+{
+	"str": [
+		"f o o",
+		"b a r",
+		"foo bar",
+		"bar foo"
+	]
+}
diff --git a/aos/starter/BUILD b/aos/starter/BUILD
index 5690bda..8d3f4f5 100644
--- a/aos/starter/BUILD
+++ b/aos/starter/BUILD
@@ -226,6 +226,9 @@
     srcs = [
         "irq_affinity.cc",
     ],
+    data = [
+        "//aos/starter:rockpi_config.json",
+    ],
     visibility = ["//visibility:public"],
     deps = [
         ":irq_affinity_lib",
diff --git a/aos/starter/irq_affinity.cc b/aos/starter/irq_affinity.cc
index 3f32ec9..5c70e9e 100644
--- a/aos/starter/irq_affinity.cc
+++ b/aos/starter/irq_affinity.cc
@@ -14,6 +14,8 @@
 
 DEFINE_string(user, "",
               "Starter runs as though this user ran a SUID binary if set.");
+DEFINE_string(irq_config, "rockpi_config.json",
+              "File path of rockpi configuration");
 
 namespace aos {
 
@@ -265,117 +267,10 @@
   aos::FlatbufferDetachedBuffer<aos::Configuration> config =
       aos::configuration::ReadConfig(FLAGS_config);
 
-  // TODO(austin): File instead of hard-coded JSON.
   aos::FlatbufferDetachedBuffer<aos::starter::IrqAffinityConfig>
       irq_affinity_config =
-          aos::JsonToFlatbuffer<aos::starter::IrqAffinityConfig>(
-              R"json({
-  "irqs": [
-    {
-      "name": "ttyS2",
-      "affinity": [1]
-    },
-    {
-      "name": "dw-mci",
-      "affinity": [1]
-    },
-    {
-      "name": "mmc1",
-      "affinity": [1]
-    },
-    {
-      "name": "rkisp1",
-      "affinity": [2]
-    },
-    {
-      "name": "ff3c0000.i2c",
-      "affinity": [2]
-    },
-    {
-      "name": "ff3d0000.i2c",
-      "affinity": [2]
-    },
-    {
-      "name": "ff6e0000.dma-controller",
-      "affinity": [0]
-    },
-    {
-      "name": "ff1d0000.spi",
-      "affinity": [0]
-    },
-    {
-      "name": "eth0",
-      "affinity": [1]
-    }
-  ],
-  "kthreads": [
-    {
-      "name": "irq/*-ff940000.hdmi",
-      "scheduler": "SCHEDULER_OTHER"
-    },
-    {
-      "name": "irq/*-rockchip_usb2phy",
-      "scheduler": "SCHEDULER_OTHER"
-    },
-    {
-      "name": "irq/*-mmc0",
-      "scheduler": "SCHEDULER_OTHER"
-    },
-    {
-      "name": "irq/*-mmc1",
-      "scheduler": "SCHEDULER_OTHER"
-    },
-    {
-      "name": "irq/*-fe320000.mmc cd",
-      "scheduler": "SCHEDULER_OTHER"
-    },
-    {
-      "name": "irq/*-vc4 crtc",
-      "scheduler": "SCHEDULER_OTHER"
-    },
-    {
-      "name": "irq/*-rkisp1",
-      "scheduler": "SCHEDULER_FIFO",
-      "priority": 60,
-      "affinity": [2]
-    },
-    {
-      "name": "irq/*-ff3c0000.i2c",
-      "scheduler": "SCHEDULER_FIFO",
-      "priority": 51,
-      "affinity": [2]
-    },
-    {
-      "name": "irq/*-adis16505",
-      "scheduler": "SCHEDULER_FIFO",
-      "priority": 59,
-      "affinity": [0]
-    },
-    {
-      "name": "irq/*-ff6e0000.dma-controller",
-      "scheduler": "SCHEDULER_FIFO",
-      "priority": 59,
-      "affinity": [0]
-    },
-    {
-      "name": "spi0",
-      "scheduler": "SCHEDULER_FIFO",
-      "priority": 57,
-      "affinity": [0]
-    },
-    {
-      "name": "irq/*-eth0",
-      "scheduler": "SCHEDULER_FIFO",
-      "priority": 10,
-      "affinity": [1]
-    },
-    {
-      "name": "irq/*-rockchip_thermal",
-      "scheduler": "SCHEDULER_FIFO",
-      "priority": 1
-    }
-  ]
-})json");
+          aos::JsonFileToFlatbuffer<aos::starter::IrqAffinityConfig>(
+              FLAGS_irq_config);
 
   aos::ShmEventLoop shm_event_loop(&config.message());
 
diff --git a/aos/starter/rockpi_config.json b/aos/starter/rockpi_config.json
new file mode 100644
index 0000000..d4cbaae
--- /dev/null
+++ b/aos/starter/rockpi_config.json
@@ -0,0 +1,107 @@
+{
+  "irqs": [
+    {
+      "name": "ttyS2",
+      "affinity": [1]
+    },
+    {
+      "name": "dw-mci",
+      "affinity": [1]
+    },
+    {
+      "name": "mmc1",
+      "affinity": [1]
+    },
+    {
+      "name": "rkisp1",
+      "affinity": [2]
+    },
+    {
+      "name": "ff3c0000.i2c",
+      "affinity": [2]
+    },
+    {
+      "name": "ff3d0000.i2c",
+      "affinity": [2]
+    },
+    {
+      "name": "ff6e0000.dma-controller",
+      "affinity": [0]
+    },
+    {
+      "name": "ff1d0000.spi",
+      "affinity": [0]
+    },
+    {
+      "name": "eth0",
+      "affinity": [1]
+    }
+  ],
+  "kthreads": [
+    {
+      "name": "irq/*-ff940000.hdmi",
+      "scheduler": "SCHEDULER_OTHER"
+    },
+    {
+      "name": "irq/*-rockchip_usb2phy",
+      "scheduler": "SCHEDULER_OTHER"
+    },
+    {
+      "name": "irq/*-mmc0",
+      "scheduler": "SCHEDULER_OTHER"
+    },
+    {
+      "name": "irq/*-mmc1",
+      "scheduler": "SCHEDULER_OTHER"
+    },
+    {
+      "name": "irq/*-fe320000.mmc cd",
+      "scheduler": "SCHEDULER_OTHER"
+    },
+    {
+      "name": "irq/*-vc4 crtc",
+      "scheduler": "SCHEDULER_OTHER"
+    },
+    {
+      "name": "irq/*-rkisp1",
+      "scheduler": "SCHEDULER_FIFO",
+      "priority": 60,
+      "affinity": [2]
+    },
+    {
+      "name": "irq/*-ff3c0000.i2c",
+      "scheduler": "SCHEDULER_FIFO",
+      "priority": 51,
+      "affinity": [2]
+    },
+    {
+      "name": "irq/*-adis16505",
+      "scheduler": "SCHEDULER_FIFO",
+      "priority": 59,
+      "affinity": [0]
+    },
+    {
+      "name": "irq/*-ff6e0000.dma-controller",
+      "scheduler": "SCHEDULER_FIFO",
+      "priority": 59,
+      "affinity": [0]
+    },
+    {
+      "name": "spi0",
+      "scheduler": "SCHEDULER_FIFO",
+      "priority": 57,
+      "affinity": [0]
+    },
+    {
+      "name": "irq/*-eth0",
+      "scheduler": "SCHEDULER_FIFO",
+      "priority": 10,
+      "affinity": [1]
+    },
+    {
+      "name": "irq/*-rockchip_thermal",
+      "scheduler": "SCHEDULER_FIFO",
+      "priority": 1
+    }
+  ]
+}