Exemplo n.º 1
0
 private dynamic GetJsonFromYaml(string data)
 {
     using (var reader = new StringReader(data))
     {
         var deserializer = new Deserializer();
         var yamlObject   = deserializer.Deserialize(reader);
         var sb           = new SerializerBuilder();
         sb.JsonCompatible();
         sb.DisableAliases();
         var    serializer = sb.Build();
         string jsonData   = serializer.Serialize(yamlObject);
         return(GetJson(jsonData));
     }
 }
        public String DeserializeAndSerialize(String defaultRoot, String path, IDictionary <String, Object> mustacheContext, CancellationToken cancellationToken)
        {
            Int32 fileCount = 0;

            // Load the target file.
            path = m_fileProvider.ResolvePath(defaultRoot: defaultRoot, path: path);
            PipelineFile <Process> processFile = LoadFile <Process, ProcessConverter>(path, mustacheContext, cancellationToken, ref fileCount);
            Process process = processFile.Object;

            ResolveTemplates(process, defaultRoot: processFile.Directory, cancellationToken: cancellationToken, fileCount: ref fileCount);

            // Serialize
            SerializerBuilder serializerBuilder = new SerializerBuilder();

            serializerBuilder.DisableAliases();
            serializerBuilder.WithTypeConverter(new ProcessConverter());
            Serializer serializer = serializerBuilder.Build();

            return(serializer.Serialize(process));
        }
Exemplo n.º 3
0
        public static DeploymentFile Read(string path, bool outputProcessedJson)
        {
            DeploymentFile deploymentFile = null;

            using (FileStream fileStream = new FileStream(path, FileMode.Open))
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    // Convert to JSON
                    var serializerBuilder = new SerializerBuilder();
                    serializerBuilder.JsonCompatible();
                    serializerBuilder.DisableAliases();
                    var serializer   = serializerBuilder.Build();
                    var deserializer = new Deserializer();
                    var writer       = new StreamWriter(stream);

                    serializer.Serialize(writer, deserializer.Deserialize(new StreamReader(fileStream)));
                    writer.Flush();

                    stream.Seek(0, SeekOrigin.Begin);
                    if (outputProcessedJson)
                    {
                        using (var file = new FileStream(Path.GetFileNameWithoutExtension(path) + "_processed.json", FileMode.OpenOrCreate))
                        {
                            stream.CopyTo(file);
                        }
                        stream.Seek(0, SeekOrigin.Begin);
                    }

                    // Deserialize with the contract
                    DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(DeploymentFile));
                    deploymentFile = jsonSerializer.ReadObject(stream) as DeploymentFile;
                }
            }
            deploymentFile.FileName = path;
            return(deploymentFile);
        }