Commits

Kamyar Inanloo committed f70b7b4

Beta version!

Comments (0)

Files changed (7)

+
+Microsoft Visual Studio Solution File, Format Version 11.00
+# Visual Studio 2010
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CuteYaml", "CuteYaml\CuteYaml.csproj", "{8CAC0E70-1C58-461B-86CA-2392E0156D3C}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestYaml", "TestYaml\TestYaml.csproj", "{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Debug|Mixed Platforms = Debug|Mixed Platforms
+		Debug|x86 = Debug|x86
+		Release|Any CPU = Release|Any CPU
+		Release|Mixed Platforms = Release|Mixed Platforms
+		Release|x86 = Release|x86
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Release|Any CPU.Build.0 = Release|Any CPU
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+		{8CAC0E70-1C58-461B-86CA-2392E0156D3C}.Release|x86.ActiveCfg = Release|Any CPU
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Debug|Any CPU.ActiveCfg = Debug|x86
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Debug|Mixed Platforms.Build.0 = Debug|x86
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Debug|x86.ActiveCfg = Debug|x86
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Debug|x86.Build.0 = Debug|x86
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Release|Any CPU.ActiveCfg = Release|x86
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Release|Mixed Platforms.ActiveCfg = Release|x86
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Release|Mixed Platforms.Build.0 = Release|x86
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Release|x86.ActiveCfg = Release|x86
+		{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}.Release|x86.Build.0 = Release|x86
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal

CuteYaml/CuteYaml.csproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{8CAC0E70-1C58-461B-86CA-2392E0156D3C}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>CuteYaml</RootNamespace>
+    <AssemblyName>CuteYaml</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Yaml.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

CuteYaml/Properties/AssemblyInfo.cs

+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("CuteYaml")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("CuteYaml")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("a5ce44ba-d482-4b73-adc3-068a222aef23")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.IO;
+using System.Collections;
+using System.Dynamic;
+
+namespace CuteYaml
+{
+    public class Yaml
+    {
+        private int level = 0;
+
+        public string Dump(object data)
+        {
+            using (var writer = new StringWriter())
+            {
+                if (data.GetType().IsArray)
+                {
+                    var array = data as Array;
+                    foreach (var item in array)
+                    {
+                        if (item.GetType().IsPrimitive || item.GetType() == typeof(string))
+                        {
+                            writer.WriteLine("{0}- {1}", new string('\t', level), item);
+                        }
+                        else
+                        {
+                            writer.WriteLine("{0}- {1}", new string('\t', level), Dump(item));
+                        }
+                    }
+                }
+                else
+                {
+                    var props = data.GetType().GetProperties();
+                    foreach (var item in props)
+                    {
+                        if (item.PropertyType.IsPrimitive || item.PropertyType == typeof(string))
+                        {
+                            writer.WriteLine("{0}{1}: {2}", new string('\t', level), item.Name, item.GetValue(data, null));
+                        }
+                        else if (item.PropertyType.IsArray)
+                        {
+                            var array = item.GetValue(data, null) as Array;
+                            writer.WriteLine("{0}{1}: ", new string('\t', level), item.Name);
+                            foreach (var element in array)
+                            {
+                                if (element.GetType().IsPrimitive || element.GetType() == typeof(string))
+                                {
+                                    writer.WriteLine("\t{0}- {1}", new string('\t', level), element);
+                                }
+                                else
+                                {
+                                    writer.WriteLine("\t-");
+                                    level++;
+                                    writer.WriteLine(Dump(element));
+                                    level--;
+                                }
+                            }
+                        }
+                        else
+                        {
+                            writer.WriteLine("{0}{1}:", new string('\t', level), item.Name);
+                            level++;
+                            writer.Write(this.Dump(item.GetValue(data, null)));
+                            level--;
+                        }
+                    }
+                }
+                return writer.ToString();
+            }
+        }
+
+        public object Load(string yaml)
+        {
+            var objectStack = new Stack<object>();
+            var currentList = new List<object>();
+            var currentDict = new Dictionary<string, object>();
+            var keyStack = new Stack<string>();
+
+            using (var reader = new StringReader(yaml))
+            {
+                string line = reader.ReadLine();
+
+                while (line != null)
+                {
+                    var depth = line.Length - line.TrimStart('\t').Length;
+                    line = line.TrimStart('\t');
+                    if (depth > objectStack.Count)
+                    {
+                        if (currentList.Count > 0)
+                        {
+                            var list = new List<object>(currentList);
+                            objectStack.Push(list);
+                            currentList.Clear();
+                        }
+                        else if (currentDict.Count() > 0)
+                        {
+                            var dict = new Dictionary<string, object>();
+                            foreach (var pair in currentDict)
+                            {
+                                dict[pair.Key] = pair.Value;
+                            }
+                            objectStack.Push(dict);
+                            currentDict.Clear();
+                        }
+                    }
+                    else
+                    {
+                        while (depth < objectStack.Count)
+                        {
+                            var prevItem = objectStack.Pop();
+                            object currentItem = null;
+                            if (currentList.Count > 0)
+                            {
+                                currentItem = currentList.ToArray();
+                                currentList.Clear();
+                            }
+                            else if (currentDict.Count > 0)
+                            {
+                                currentItem = new ExpandoObject();
+                                foreach (var pair in currentDict)
+                                {
+                                    (currentItem as IDictionary<string, object>)[pair.Key] = pair.Value;
+                                }
+                                currentDict.Clear();
+                            }
+                            if (prevItem is List<object>)
+                            {
+                                (prevItem as List<object>).Add(currentItem);
+                                currentList = prevItem as List<object>;
+                            }
+                            else
+                            {
+                                (prevItem as IDictionary<string, object>)[keyStack.Pop()] = currentItem;
+                                currentDict = prevItem as Dictionary<string, object>;
+                            }
+                        }
+                    }
+                    if (line[0] == '-')
+                    {
+                        if (line.Trim().Length > 1)
+                        {
+                            currentList.Add(Parse(line.Substring(2)));
+                        }
+                    }
+                    else
+                    {
+                        var keyvalue = line.Split(':');
+                        if (keyvalue[1].Trim() != string.Empty)
+                        {
+                            currentDict[keyvalue[0]] = Parse(keyvalue[1].Trim());
+                        }
+                        else
+                        {
+                            keyStack.Push(keyvalue[0]);
+                        }
+                    }
+
+                    line = reader.ReadLine();
+                }
+                while (objectStack.Count > 0)
+                {
+                    var prevItem = objectStack.Pop();
+                    object currentItem = null;
+                    if (currentList.Count > 0)
+                    {
+                        currentItem = currentList.ToArray();
+                        currentList.Clear();
+                    }
+                    else if (currentDict.Count > 0)
+                    {
+                        currentItem = new ExpandoObject();
+                        foreach (var pair in currentDict)
+                        {
+                            (currentItem as IDictionary<string, object>)[pair.Key] = pair.Value;
+                        }
+                        currentDict.Clear();
+                    }
+                    if (prevItem is List<object>)
+                    {
+                        (prevItem as List<object>).Add(currentItem);
+                        currentList = prevItem as List<object>;
+                    }
+                    else
+                    {
+                        (prevItem as IDictionary<string, object>)[keyStack.Pop()] = currentItem;
+                        currentDict = prevItem as Dictionary<string, object>;
+                    }
+                }
+
+                if (currentList.Count > 0)
+                {
+                    return currentList.ToArray();
+                }
+                else
+                {
+                    var obj = new ExpandoObject();
+                    foreach (var pair in currentDict)
+                    {
+                        (obj as IDictionary<string, object>)[pair.Key] = pair.Value;
+                    }
+                    return obj;
+                }
+            }
+        }
+
+        private object Parse(string item)
+        {
+            int i;
+            long l;
+            bool b;
+            float f;
+            double d;
+            if (int.TryParse(item, out i))
+            {
+                return i;
+            }
+            else if (long.TryParse(item, out l))
+            {
+                return l;
+            }
+            else if (float.TryParse(item, out f))
+            {
+                return f;
+            }
+            else if (double.TryParse(item, out d))
+            {
+                return d;
+            }
+            else if (bool.TryParse(item, out b))
+            {
+                return b;
+            }
+            else
+            {
+                return item;
+            }
+        }
+    }
+}

TestYaml/Program.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using CuteYaml;
+
+namespace TestYaml
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            var y = new Yaml();
+            //var s = y.Dump(new { a = 5, b = "test", c = new int[] { 6, 5, 4 }, d = new object[] { new { x = 5, y = 9 } , new {g = 6, h = "test"}} , xyz = 657});
+            //var o = y.Load(y.Dump(new int[] { 1, 2, 3 }));
+            var s = y.Dump(new { a = 5, b = 6, c = new { x = 67, y = "test", z = new int[] { 8, 9, 0 } }, d = "john" });
+            dynamic o = y.Load(s);
+
+            Console.WriteLine(o.d);
+            Console.ReadLine();
+        }
+    }
+}

TestYaml/Properties/AssemblyInfo.cs

+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("TestYaml")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("Microsoft")]
+[assembly: AssemblyProduct("TestYaml")]
+[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("43ea0828-1885-4601-91a2-4ae67a7f0f61")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

TestYaml/TestYaml.csproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProductVersion>8.0.30703</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{5B0FF58D-9A79-4C9D-BBEF-CFC348A8A255}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>TestYaml</RootNamespace>
+    <AssemblyName>TestYaml</AssemblyName>
+    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <TargetFrameworkProfile>Client</TargetFrameworkProfile>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <PlatformTarget>x86</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <PlatformTarget>x86</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\CuteYaml\CuteYaml.csproj">
+      <Project>{8CAC0E70-1C58-461B-86CA-2392E0156D3C}</Project>
+      <Name>CuteYaml</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>