DLPrimitives
json_helpers.hpp
1 #pragma once
2 #include <dlprim/json.hpp>
3 #include <dlprim/definitions.hpp>
4 
5 namespace dlprim {
6 namespace utils {
7  template<typename T,size_t S>
8  T parse_enum(json::value const &v,std::string const &name,char const *(&names)[S],T def)
9  {
10  if(v.find(name).is_undefined())
11  return def;
12  std::string val = v.get<std::string>(name);
13  for(size_t i=0;i<S;i++) {
14  if(val == names[i])
15  return static_cast<T>(i);
16  }
17  throw ValidationError("Invalid value " + val + " for filed " + name);
18  }
19  template<typename T,size_t S>
20  void get_1dNd_from_json(json::value const &v,std::string const &name,T (&vals)[S],bool required=false)
21  {
22  json::value const &tmp = v.find(name);
23  if(tmp.is_undefined()) {
24  if(required)
25  throw ValidationError("Missing value in json " + name);
26  return;
27  }
28  else if(tmp.type()==json::is_number) {
29  T val = tmp.get_value<T>();
30  for(size_t i=0;i<S;i++)
31  vals[i] = val;
32  }
33  else if(tmp.type()==json::is_array) {
34  auto ar = tmp.get_value<std::vector<T> >();
35  if(ar.size() != S)
36  throw ValidationError("Array size of filed " + name + " must be " + std::to_string(S));
37  for(size_t i=0;i<S;i++)
38  vals[i] = ar[i];
39  }
40  else {
41  throw ValidationError("Invalid filed value for " + name);
42  }
43  }
44  inline StandardActivations activation_from_json(json::value const &v)
45  {
46  return activation_from_name(v.get("activation","identity"));
47  }
48 
49 } // util
50 } // json
array value
Definition: json.hpp:601
Mane namespace.
Definition: context.hpp:9
StandardActivations
Parameterless Activations that can be embedded to general kernels like inner product or convolution...
Definition: definitions.hpp:266
numeric value
Definition: json.hpp:598