• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using System;
11 using System.IO;
12 using System.Reflection;
13 
14 namespace Google.Protobuf.ProtoDump
15 {
16     /// <summary>
17     /// Small utility to load a binary message and dump it in JSON format.
18     /// </summary>
19     internal class Program
20     {
Main(string[] args)21         private static int Main(string[] args)
22         {
23             if (args.Length != 2)
24             {
25                 Console.Error.WriteLine("Usage: Google.Protobuf.JsonDump <descriptor type name> <input data>");
26                 Console.Error.WriteLine("The descriptor type name is the fully-qualified message name,");
27                 Console.Error.WriteLine("including assembly e.g. ProjectNamespace.Message,Company.Project");
28                 return 1;
29             }
30             Type type = Type.GetType(args[0]);
31             if (type == null)
32             {
33                 Console.Error.WriteLine("Unable to load type {0}.", args[0]);
34                 return 1;
35             }
36             if (!typeof(IMessage).GetTypeInfo().IsAssignableFrom(type))
37             {
38                 Console.Error.WriteLine("Type {0} doesn't implement IMessage.", args[0]);
39                 return 1;
40             }
41             IMessage message = (IMessage) Activator.CreateInstance(type);
42             using (var input = File.OpenRead(args[1]))
43             {
44                 message.MergeFrom(input);
45             }
46             Console.WriteLine(message);
47             return 0;
48         }
49     }
50 }