{"intMap":["4":500,"0":100,"1":200,"2":300,"3":400]}

The above json is declared invalid by several json validators.

ZZZZZZZZZZZZZZZZZZZZZZ  It is generated by the jtest.proto:

syntax = "proto3";
package jtest;

message Jmap {
map<int32, int32> int_map = 2; 
}

ZZZZZZZZZZZZZZZZZZZZZZZ make_json.cpp :

#include <getopt.h>
#include <chrono>
#include <iostream>
#include <fstream>
#include <memory>
#include <random>
#include <string>
#include <thread>

#include <google/protobuf/text_format.h>
#include <grpc/grpc.h>
#include <grpc++/channel.h>
#include <grpc++/client_context.h>
#include <grpc++/create_channel.h>
#include <grpc++/security/credentials.h>
#include "netopt/proto/json2string.h"
#include "jtest.grpc.pb.h"

using grpc::Channel;
using grpc::ClientContext;
using grpc::ClientReader;
using grpc::ClientReaderWriter;
using grpc::ClientWriter;
using grpc::Status;

using jtest::Jmap;

using ::google::protobuf::Map;
using ::google::protobuf::int32;
typedef ::google::protobuf::Map< ::google::protobuf::int32, 
::google::protobuf::int32 > IntMap;
typedef ::google::protobuf::MapPair< ::google::protobuf::int32, 
::google::protobuf::int32 > IntPair;

#define MAP_COUNT 5


Jmap MakeJmap() {
  Jmap m;
  IntMap* map_p =m.mutable_int_map();
  for(int i = 0; i < MAP_COUNT; i++) {
    IntPair pair1(i, (i + 1)*100 );
    map_p->insert(pair1);
  }
  return m;
}

int main(int argc, char** argv) {
  std::string m_file_name = "jmap.json";
  std::string a_file_name = "array.json";
  Jmap j = MakeJmap();

  google::protobuf::util::Json2String j2s;
  google::protobuf::util::JsonOptions jo;
  //  jo.add_whitespace = true;
  jo.always_print_primitive_fields = true;
  
  std::ofstream m_doc(m_file_name);
  if(!m_doc.is_open()) {
    GM_COUT << "Could not open " << m_file_name << ", EXITING!!!" << 
std::endl;
    return 1;
  }
  std::string m_line = j2s.ToJson(j, jo);
  if(m_line.empty()) {
    GM_COUT << "Could not print to string, EXITING!!!" << std::endl;
    return 1;
  }
  m_doc << m_line;
  m_doc.close();

  return 0;
}

ZZZZZZZZZZZZZZZZZZZZ where json2string.h is:

#include <google/protobuf/util/json_util.h>

#include <list>
#include <string>

#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/util/type_resolver.h>
#include <google/protobuf/util/type_resolver_util.h>
#include "common/gen_headers/macros.h"
#include "common/gen_headers/typedefs.h"

namespace google {
  namespace protobuf {
    namespace util {
      static const char kTypeUrlPrefix[] = "type.googleapis.com";

      static string GetTypeUrl(const Descriptor* message) {
return string(kTypeUrlPrefix) + "/" + message->full_name();
      }

      // As functions defined in json_util.h are just thin wrappers around 
the
      // JSON conversion code in //net/proto2/util/converter, in this test 
we
      // only cover some very basic cases to make sure the wrappers have 
forwarded
      // parameters to the underlying implementation correctly. More 
detailed
      // tests are contained in the //net/proto2/util/converter directory.
      class Json2String {
      public:
Json2String() {
 resolver_.reset(NewTypeResolverForDescriptorPool(kTypeUrlPrefix, 
DescriptorPool::generated_pool()));
}

string ToJson(const Message& message, const JsonOptions& options) {
 string result;
 Status status =
   BinaryToJsonString(resolver_.get(),
      GetTypeUrl(message.GetDescriptor()),
      message.SerializeAsString(), &result, options);
 if(!status.ok()) {
   GM_COUT << status.ToString() << std::endl;
 }
 return result;
}

bool FromJson(const string& json, Message* message) {
 string binary;
 util::Status status = 
   JsonToBinaryString(resolver_.get(), 
GetTypeUrl(message->GetDescriptor()), json, &binary);
 if(!status.ok()) {
   GM_COUT << status.ToString() << std::endl;
   return false;
 }

 return message->ParseFromString(binary);
}

google::protobuf::scoped_ptr<TypeResolver> resolver_;
      };
    } // namespace util
  } // namespace protobuf
} // namespace google


-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to