public static void Main(string[] args)
        {
            try
            {
                var parsed = Args.Parse <JsonTransformArguments>(args);
                //Console.WriteLine("You entered string '{0}' and int '{1}'", parsed.StringArg, parsed.IntArg);
                var logger    = new ConsoleJsonTransformationLogger();
                var transform = new JsonTransformation(parsed.TransformFile, logger);

                if (File.Exists(parsed.DestinationFile))
                {
                    File.Delete(parsed.DestinationFile);
                }

                //File.Copy(parsed.SourceFile, parsed.DestinationFile);
                var result = transform.Apply(parsed.SourceFile);
                using (var destFile = File.OpenWrite(parsed.DestinationFile))
                    result.CopyTo(destFile);

                Console.WriteLine($"Transform completed successfully into {parsed.DestinationFile}");
            }
            catch (ArgException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ArgUsage.GenerateUsageFromTemplate <JsonTransformArguments>());
            }
        }
        public void DoTransform(string sourceFile, string transformFile, string outputFile)
        {
            _jsonTransformationLogger.LogMessage("Attempting to perform transform.");
            _jsonTransformationLogger.LogMessage($"Transforming source file: {sourceFile} using file {transformFile}");

            var absoluteSourcePath            = Path.GetFullPath(sourceFile);
            JsonTransformation transformation = CreateJsonTransformation(transformFile);

            // apply transform
            using (Stream outputStream = transformation.Apply(absoluteSourcePath))
            {
                using (StreamReader streamReader = new StreamReader(outputStream))
                {
                    string finalText = streamReader.ReadToEnd();

                    if (string.IsNullOrWhiteSpace(outputFile))
                    {
                        outputFile = defaultOutputFilename;
                        _jsonTransformationLogger.LogWarning($"No output filename was given. Using default filename of \"{defaultOutputFilename}\"");
                    }

                    _jsonTransformationLogger.LogMessage($"Writing transformed file to {outputFile}");
                    File.WriteAllText($"{outputFile}", finalText);
                }
            }

            _jsonTransformationLogger.LogMessage("Done and done.");
        }
 protected internal override void TransformInternal(string destination, string source, string transformation)
 {
     using (var stream = new JsonTransformation(transformation).Apply(source))
     {
         this.SaveToFile(destination, stream);
     }
 }
 public static string ApplyTransform(this string source, string transform)
 {
     using (var transformStream = transform.AsStream())
         using (var sourceStream = source.AsStream())
         {
             JsonTransformation transformation = new JsonTransformation(transformStream, null);
             var result = transformation.Apply(sourceStream);
             return(result.ReadAsString());
         }
 }
        private void TryTransformTest(string sourceString, string transformString, bool shouldTransformSucceed)
        {
            using (var transformStream = this.GetStreamFromString(transformString))
                using (var sourceStream = this.GetStreamFromString(sourceString))
                {
                    JsonTransformation transform = new JsonTransformation(transformStream, this.logger);

                    this.AssertTransformSucceeds(() => transform.Apply(sourceStream), shouldTransformSucceed);
                }
        }
示例#6
0
        public void RemovePropertyTest()
        {
            const string original = "{\"x\":\"originalValue\"}";
              const string column = "json";
              const string code = "((JObject)json).Remove(\"x\");";

              var jsonScript = new JsonTransformation(column, code, format: false);
              var result = jsonScript.Apply(original, null);
              Assert.AreEqual("{}", result);
        }
示例#7
0
        public void ApplyTest()
        {
            const string original = "{\"x\":\"originalValue\"}";
              const string column = "json";
              const string code = "json.x = \"modifiedValue\";";

              var jsonScript = new JsonTransformation(column, code, format: false);
              var result = jsonScript.Apply(original, null);
              Assert.AreEqual("{\"x\":\"modifiedValue\"}", result);
        }
        internal static MemoryStream ApplyJsonTransformations(Stream configInputStream, Stream transformInputStream)
        {
            transformInputStream = EnvironmentHelper.ExpandEnvironmentVariablesInConfig(transformInputStream);

            JsonTransformation jsonTransformation = new JsonTransformation(transformInputStream);

            using (Stream configResultStream = jsonTransformation.Apply(configInputStream))
            {
                MemoryStream configOutputStream = new MemoryStream();
                configResultStream.CopyTo(configOutputStream);
                configOutputStream.Seek(0, SeekOrigin.Begin);
                return(configOutputStream);
            }
        }
        public void ReadOnly()
        {
            const string TransformSourceString = @"{
                                                        '@jdt.rename' : {
                                                            'A' : 'Astar'
                                                        }
                                                    }";

            // create temporary files to use for the source and transform
            using (ReadOnlyTempFile tempSourceFilePath = new ReadOnlyTempFile(SimpleSourceString))
                using (ReadOnlyTempFile tempTransformFilePath = new ReadOnlyTempFile(TransformSourceString))
                {
                    // apply transform
                    JsonTransformation transformation = new JsonTransformation(tempTransformFilePath.FilePath, this.logger);

                    this.AssertTransformSucceeds(() => transformation.Apply(tempSourceFilePath.FilePath), true);
                }
        }
        private static void BaseTransformTest(string inputsDirectory, string testName)
        {
            // Removes the test name to find the source file
            string sourceName = Path.GetFileNameWithoutExtension(testName);

            var transformation = new JsonTransformation(inputsDirectory + testName + ".Transform.json");

            // Read the resulting stream into a JObject to compare
            using (Stream result = transformation.Apply(inputsDirectory + sourceName + ".Source.json"))
                using (StreamReader streamReader = new StreamReader(result))
                    using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
                    {
                        var expected = JObject.Parse(File.ReadAllText(inputsDirectory + testName + ".Expected.json"));

                        var transformed = JObject.Load(jsonReader);

                        Assert.True(JObject.DeepEquals(expected, transformed));
                    }
        }
示例#11
0
 public void ThrowAndLogException()
 {
     string transformString = @"{ 
                                  '@jdt.invalid': false 
                                }";
     using (var transformStream = this.GetStreamFromString(transformString))
     using (var sourceStream = this.GetStreamFromString(SimpleSourceString))
     {
         JsonTransformation transform = new JsonTransformation(transformStream, this.logger);
         var exception = Record.Exception(() => transform.Apply(sourceStream));
         Assert.NotNull(exception);
         Assert.IsType<JdtException>(exception);
         var jdtException = exception as JdtException;
         Assert.Contains("invalid", jdtException.Message);
         Assert.Equal(ErrorLocation.Transform, jdtException.Location);
         Assert.Equal(2, jdtException.LineNumber);
         Assert.Equal(56, jdtException.LinePosition);
     }
 }
示例#12
0
        /// <inheritdoc/>
        public bool Transform(string sourcePath, string transformPath, string destinationPath)
        {
            if (string.IsNullOrWhiteSpace(sourcePath))
            {
                throw new ArgumentException($"{nameof(sourcePath)} cannot be null or whitespace");
            }

            if (string.IsNullOrWhiteSpace(transformPath))
            {
                throw new ArgumentException($"{nameof(transformPath)} cannot be null or whitespace");
            }

            if (string.IsNullOrWhiteSpace(destinationPath))
            {
                throw new ArgumentException($"{nameof(destinationPath)} cannot be null or whitespace");
            }

            if (!File.Exists(sourcePath))
            {
                throw new FileNotFoundException(Resources.Resources.ErrorMessage_SourceFileNotFound, sourcePath);
            }

            if (!File.Exists(transformPath))
            {
                throw new FileNotFoundException(Resources.Resources.ErrorMessage_TransformFileNotFound, transformPath);
            }

            var transformation = new JsonTransformation(transformPath, this.logger);

            try
            {
                using (Stream result = transformation.Apply(sourcePath))
                {
                    return(this.TrySaveToFile(result, sourcePath, destinationPath));
                }
            }
            catch
            {
                // JDT exceptions are handled by it's own logger
                return(false);
            }
        }
示例#13
0
        private void TryTransformTest(string sourceString, string transformString, bool shouldTransformSucceed)
        {
            using (var transformStream = this.GetStreamFromString(transformString))
            using (var sourceStream = this.GetStreamFromString(sourceString))
            {
                JsonTransformation transform = new JsonTransformation(transformStream, this.logger);
                Stream result = null;

                var exception = Record.Exception(() => result = transform.Apply(sourceStream));

                if (shouldTransformSucceed)
                {
                    Assert.NotNull(result);
                    Assert.Null(exception);
                }
                else
                {
                    Assert.Null(result);
                    Assert.NotNull(exception);
                    Assert.IsType<JdtException>(exception);
                }
            }
        }
示例#14
0
        public Stream Transform(Stream input, Stream transform)
        {
            var jdt = new JsonTransformation(transform, _logger);

            return(jdt.Apply(input));
        }
示例#15
0
        public AutoMapper(JObject mapping)
        {
            _mappingFile = mapping.ToObject <JsonMapping>();

            transform = new JsonTransformation(_mappingFile);
        }
示例#16
0
        public AutoMapper(string mapping)
        {
            _mappingFile = JsonConvert.DeserializeObject <JsonMapping>(mapping);

            transform = new JsonTransformation(_mappingFile);
        }