2014-03-24 123 views
1

我想知道是否有人可以幫助我一個類可以採取任何類型的例子,並將其序列化爲字符串像JSON或分隔符字符串,並回到它的原始類型。文本文件中的配置設置是一行值,帶有分隔符。一種方法來反序列化和序列化對象的靜態類

例如,我有一個對象或類是一個配置設置。所以,當讀取config/text文件中的一行時,我想將其序列化爲一個ConfigurationSetting對象。任何幫助將不勝感激,並提前致謝!我曾與JSOn見過,但找不到它的參考,所以忘記了它是如何處理的。

這裏是配置文件的一個例子:

線形成其計算方法如下:ZZZVISA *** |帕布克斯VISA MAIN.PDF | LITERAL | TRUE

因此,這是不可能的,因爲我沒有帶有值的屬性名稱,.net知道如何處理它?

+0

請不要包含關於問題標題中使用的語言的信息,除非在沒有它的情況下沒有意義。標籤用於此目的。 –

回答

1

你可以使用類似JSON.Net的東西來序列化你的對象到/從JSON。

文檔中的Basic Reading and Writing頁面顯示瞭如何從.NET對象讀取和寫入JSON。


如果你只是想打破了分隔的信息,並建立它,你可能還需要考慮使用String.SplitString.Join。這些方法允許您將分隔文本分解爲數組,然後將數組重新加入到分隔文本中。

+0

儘管它們不是JSON格式。這是一個被刪除的文本文件。這是可能的,還是不可以? – Casey

+0

它會不工作,因爲我需要從每個值的屬性名稱,以便能夠序列化它? – Casey

+0

@Casey你只是想使用string.Split然後? –

0

您可以使用string.format以該格式序列化數據。要反序列化它,你可以使用RegEx將它分成組。我有一些課程用於解析來自串行連接設備的線路。我的Parser類如下所示,它接受一個正則表達式字符串並可以返回強類型的結果。

例如

 var sp = new Parser("# (d+) VO(d+) SP(d+)\\+"); 
     int a, b, c; 
     bool ok = sp.Parse<int, int, int>("# 123 VO256 SP256+", out a, out b, out c); 

/// <summary> 
/// A strongly typed parse result containing up to 10 elements, each individually strongly typed 
/// </summary> 
public class Parser 
{ 
    Regex regex; 
    public Parser(string regularExpression) 
    { 
     this.regex = new Regex(regularExpression, RegexOptions.Compiled); 
    } 

    public bool Parse<T>(string input, out T value1) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     return ok; 
    } 

    public bool Parse<T1, T2>(string input, out T1 value1, out T2 value2) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     ok = Assign(match, 2, out value2) && ok; 
     return ok; 
    } 

    public bool Parse<T1, T2, T3>(string input, out T1 value1, out T2 value2, out T3 value3) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     ok = Assign(match, 2, out value2) && ok; 
     ok = Assign(match, 3, out value3) && ok; 
     return ok; 
    } 

    public bool Parse<T1, T2, T3, T4>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     ok = Assign(match, 2, out value2) && ok; 
     ok = Assign(match, 3, out value3) && ok; 
     ok = Assign(match, 4, out value4) && ok; 
     return ok; 
    } 

    public bool Parse<T1, T2, T3, T4, T5>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     ok = Assign(match, 2, out value2) && ok; 
     ok = Assign(match, 3, out value3) && ok; 
     ok = Assign(match, 4, out value4) && ok; 
     ok = Assign(match, 5, out value5) && ok; 
     return ok; 
    } 

    public bool Parse<T1, T2, T3, T4, T5, T6>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     ok = Assign(match, 2, out value2) && ok; 
     ok = Assign(match, 3, out value3) && ok; 
     ok = Assign(match, 4, out value4) && ok; 
     ok = Assign(match, 5, out value5) && ok; 
     ok = Assign(match, 6, out value6) && ok; 
     return ok; 
    } 

    public bool Parse<T1, T2, T3, T4, T5, T6, T7>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6, out T7 value7) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     ok = Assign(match, 2, out value2) && ok; 
     ok = Assign(match, 3, out value3) && ok; 
     ok = Assign(match, 4, out value4) && ok; 
     ok = Assign(match, 5, out value5) && ok; 
     ok = Assign(match, 6, out value6) && ok; 
     ok = Assign(match, 7, out value7) && ok; 
     return ok; 
    } 

    public bool Parse<T1, T2, T3, T4, T5, T6, T7, T8>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6, out T7 value7, out T8 value8) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     ok = Assign(match, 2, out value2) && ok; 
     ok = Assign(match, 3, out value3) && ok; 
     ok = Assign(match, 4, out value4) && ok; 
     ok = Assign(match, 5, out value5) && ok; 
     ok = Assign(match, 6, out value6) && ok; 
     ok = Assign(match, 7, out value7) && ok; 
     ok = Assign(match, 8, out value8) && ok; 
     return ok; 
    } 

    public bool Parse<T1, T2, T3, T4, T5, T6, T7, T8, T9>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6, out T7 value7, out T8 value8, out T9 value9) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     ok = Assign(match, 2, out value2) && ok; 
     ok = Assign(match, 3, out value3) && ok; 
     ok = Assign(match, 4, out value4) && ok; 
     ok = Assign(match, 5, out value5) && ok; 
     ok = Assign(match, 6, out value6) && ok; 
     ok = Assign(match, 7, out value7) && ok; 
     ok = Assign(match, 8, out value8) && ok; 
     ok = Assign(match, 9, out value9) && ok; 
     return ok; 
    } 

    public bool Parse<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(string input, out T1 value1, out T2 value2, out T3 value3, out T4 value4, out T5 value5, out T6 value6, out T7 value7, out T8 value8, out T9 value9, out T10 value10) 
    { 
     bool ok = true; 
     var match = this.regex.Match(input); 
     ok = Assign(match, 1, out value1) && ok; 
     ok = Assign(match, 2, out value2) && ok; 
     ok = Assign(match, 3, out value3) && ok; 
     ok = Assign(match, 4, out value4) && ok; 
     ok = Assign(match, 5, out value5) && ok; 
     ok = Assign(match, 6, out value6) && ok; 
     ok = Assign(match, 7, out value7) && ok; 
     ok = Assign(match, 8, out value8) && ok; 
     ok = Assign(match, 9, out value9) && ok; 
     ok = Assign(match, 10, out value10) && ok; 
     return ok; 
    } 

    private bool Assign<X>(Match match, int index, out X value) 
    { 
     if (match.Success) 
     { 
      Group group = match.Groups[index]; 
      var stringValue = group.Value; 
      var foo = TypeDescriptor.GetConverter(typeof(X)); 
      value = (X)(foo.ConvertFromInvariantString(stringValue)); 
      return true; 
     } 
     else 
     { 
      value = default(X); 
      return false; 
     } 
    } 
相關問題