Skip to content

Instantly share code, notes, and snippets.

@yojimbo87
Created September 3, 2012 15:58
Show Gist options
  • Save yojimbo87/3610260 to your computer and use it in GitHub Desktop.
Save yojimbo87/3610260 to your computer and use it in GitHub Desktop.
Raw document string parsing test 2
internal static ODocument ToDocument(string rawDocument)
{
ODocument document = new ODocument();
int atIndex = rawDocument.IndexOf('@');
int colonIndex = rawDocument.IndexOf(':');
string parsedType = "fieldName";
string lastParsedField = "";
string nesting = "";
int itemStartIndex = 0;
RecordColletionType collectionType = RecordColletionType.None;
// parse class name
if ((atIndex != -1) && (atIndex < colonIndex))
{
document.Class = rawDocument.Substring(0, atIndex);
itemStartIndex = atIndex + 1;
}
else
{
document.Class = "";
}
int i = 0;
while (i < rawDocument.Length)
{
switch (rawDocument[i])
{
case ':':
if (parsedType == "fieldName")
{
lastParsedField = rawDocument.Substring(itemStartIndex, i - itemStartIndex);
document.Fields.Add(lastParsedField, null);
parsedType = "value";
itemStartIndex = i + 1;
}
break;
case ',':
// parse previous value
switch (parsedType)
{
case "recordID":
case "value":
string value = rawDocument.Substring(itemStartIndex, i - itemStartIndex);
if (collectionType == RecordColletionType.Flat)
{
((List<string>)document.Fields[lastParsedField]).Add(rawDocument.Substring(itemStartIndex, i - itemStartIndex));
}
else if (collectionType == RecordColletionType.NestedDocuments)
{
//Dictionary<string, object> nestedDocument = ((List<Dictionary<string, object>>)document.Fields[lastParsedField]).Last();
//nestedDocument
}
else
{
if (value != "")
{
document.Fields[lastParsedField] = rawDocument.Substring(itemStartIndex, i - itemStartIndex);
}
}
itemStartIndex = i + 1;
if (parsedType == "recordID")
{
parsedType = "";
}
break;
default:
break;
}
// examine if the fieldName parsing follows
if ((parsedType != "string") && (parsedType != "recordID") && (collectionType == RecordColletionType.None))
{
parsedType = "fieldName";
itemStartIndex = i + 1;
}
break;
case '"':
if (parsedType != "string")
{
parsedType = "string";
itemStartIndex = i + 1;
}
else
{
string value = rawDocument.Substring(itemStartIndex, i - itemStartIndex);
if (collectionType == RecordColletionType.Flat)
{
((List<string>)document.Fields[lastParsedField]).Add(value);
}
else
{
document.Fields[lastParsedField] = value;
}
parsedType = "";
}
break;
case '#':
if (parsedType != "string")
{
if (parsedType != "recordID")
{
parsedType = "recordID";
itemStartIndex = i;
}
}
break;
case '(':
if (parsedType != "string")
{
// field name parsing follow since it's a nested document
parsedType = "fieldName";
itemStartIndex = i + 1;
// check if nesting takes place
if ((rawDocument[i - 1] == ':') || (rawDocument[i - 1] == '['))
{
nesting = lastParsedField + ".";
}
}
break;
case ')':
if (parsedType != "string")
{
// parse previous value
switch (parsedType)
{
case "recordID":
case "value":
string value = rawDocument.Substring(itemStartIndex, i - itemStartIndex);
if (collectionType == RecordColletionType.Flat)
{
((List<string>)document.Fields[lastParsedField]).Add(value);
}
else
{
if (value != "")
{
document.Fields[lastParsedField] = value;
}
}
parsedType = "";
break;
default:
break;
}
// check if nesting change takes place
if (collectionType == RecordColletionType.None)
{
int dotIndex = nesting.LastIndexOf('.');
if (dotIndex != -1)
{
nesting = nesting.Substring(0, dotIndex);
}
else
{
nesting = "";
}
}
parsedType = "";
}
break;
case '[':
if (parsedType != "string")
{
// check if the collection is flat or consists of nested docuemnts
if (rawDocument[i + 1] == '(')
{
collectionType = RecordColletionType.NestedDocuments;
}
else
{
collectionType = RecordColletionType.Flat;
document.Fields[lastParsedField] = new List<string>();
}
itemStartIndex = i + 1;
}
break;
case ']':
if ((parsedType != "string") && (collectionType != RecordColletionType.None))
{
switch (parsedType)
{
case "recordID":
case "value":
string value = rawDocument.Substring(itemStartIndex, i - itemStartIndex);
if (collectionType == RecordColletionType.Flat)
{
((List<string>)document.Fields[lastParsedField]).Add(value);
}
parsedType = "";
break;
default:
break;
}
// check if nesting change takes place
if (collectionType == RecordColletionType.NestedDocuments)
{
int dotIndex = nesting.LastIndexOf('.');
if (dotIndex != -1)
{
nesting = nesting.Substring(0, dotIndex);
}
else
{
nesting = "";
}
}
parsedType = "";
collectionType = RecordColletionType.None;
}
break;
default:
break;
}
i++;
// parsing of last value at the end of raw document string
if (i == rawDocument.Length)
{
switch (parsedType)
{
case "recordID":
case "value":
string value = rawDocument.Substring(itemStartIndex, i - itemStartIndex);
if (collectionType == RecordColletionType.Flat)
{
((List<string>)document.Fields[lastParsedField]).Add(value);
}
else
{
if (value != "")
{
document.Fields[lastParsedField] = value;
}
}
break;
default:
break;
}
}
}
return document;
}
}
internal enum RecordColletionType
{
None = 0,
Flat = 1,
NestedDocuments = 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment