JSON Parser minor glitches?

Jan 13, 2013 at 6:09 AM

Hi. Despite your JSON parser for .NETMF is by far the best we found on the net, using it to parse COSM triggers HTTP request seems to show a couple of small glitches that sadly prevent to use it as is.

At this time we modified following two methods this way:

protected void StoreDataInCurrentDataStructure()
        {
            if (_currentDataStructure.ObjectType == JSONObjectType.Object)
            {
                var value = (string)_dataStack.Pop();
                var name = (string)_dataStack.Pop();
                Debug.Print("DAT: Popped pair: " + name + " = " + value);
                var hashTable = (Hashtable)_currentDataStructure.Object;
                hashTable.Add(name, value);
                return;
            }
            else
            {
                var value = (string)_dataStack.Pop();
                Debug.Print("DAT: Popped value: " + value);
                var arrayList = (ArrayList)_currentDataStructure.Object;
                arrayList.Add(value);
            }
        }
        protected void StoreCurrentDataStructure()
        {
            var innerStructure = (JSONObject)_dataStructureStack.Pop();
            if (_dataStructureStack.Count == 0) return;

            _currentDataStructure = (JSONObject)_dataStructureStack.Peek();
            if (_currentDataStructure.ObjectType == JSONObjectType.Object)
            {
                var name = (string)_dataStack.Pop();
                Debug.Print("STR: Popped data: " + name);
                var hashTable = (Hashtable)_currentDataStructure.Object;
                hashTable.Add(name, innerStructure);
            }
            else
            {
                var arrayList = (ArrayList)_currentDataStructure.Object;
                arrayList.Add(innerStructure);
            }
        }

Could you please check whether they are ok?

Btw, we found this helper could be useful, too:

public object GetHierarchicalValue(Hashtable hashTable, string Path)
        {
            string[] parts = Path.Split('/');

            Hashtable htLoop = hashTable;

            foreach (var part in parts)
            {
                if (htLoop[part] is JSONObject)
                {
                    htLoop = (htLoop[part] as JSONObject).Object as Hashtable;
                }
                else
                {
                    return htLoop[part];
                }
            }

            return null;
        }

This way, you could for example get specific values using following syntax:

JSONParser parser = new JSONParser(1024);

            var ht = parser.Parse(content);

            string symbol = parser.GetHierarchicalValue(ht, "triggering_datastream/units/symbol").ToString();
            string value = parser.GetHierarchicalValue(ht, "triggering_datastream/value/value").ToString();

BTW, what about encapsulating Hashtable in parser object?

Thanks!

Coordinator
Jan 13, 2013 at 5:19 PM

Hi,

I've mentioned it a couple times in the past: this JSON parser is a minimalist implementation that met my needs when I wrote it (i.e. parsing Klout feeds at the time). It's not robust and has issues that I never got around to fixing (but I called them out ;-) If you have the time to create a pull request with the fixes you proposed, I'll integrate them as soon as I get the chance.

Thank you,

Cheers,

-Fabien.

Apr 9, 2013 at 3:20 AM
Hello,

Innovactive I really appreciate you also working to debug the issues with this JSON parser. Fabien, I'm not sur eif you understand, but even your sample code is broken and will not run.

Innovactive when I looked at your proposed fix, I can not find any changes to the StoreDataInCurrentDataStructure() method ... did you happen to paste something wrong?

This is really a great set of helpers, and I appreciate the work that has gone into it! If I can find a solution I'll make sure to post my fixes also!

Scott
Coordinator
Apr 11, 2013 at 6:44 PM
Hi Scott,

"Fabien, I'm not sur eif you understand, but even your sample code is broken and will not run."

As I mentioned it a few times in the past, I understand that the latest check-in for this parser / sample code is broken. The previous version and sample code (see the original Klout Klock check in worked). I am sorry for the inconvenience but I have no resources to focus on this library unfortunately.

-Fabien.
Apr 11, 2013 at 7:39 PM
Hello,

"As I mentioned it a few times in the past, I understand that the latest check-in for this parser / sample code is broken. The previous version and sample code (see the original Klout Klock check in worked). I am sorry for the inconvenience but I have no resources to focus on this library unfortunately."

Sorry ... when I saw the sample code with the sample "klout" JSON in it, I though you meant this sample should work.

I'm sorry it's not working right now, I've tried to go through and understand the pattern/logic that you are using, but I've missed something. The issue is where you are nearing the end of a structure, and perform an extra attempt to pop a name/value pair when there is only a name left on the stack.

I haven't spent the time to dig more and determine where the check is missing to prevent this.

Thanks for the rest of the code ... hopefully someone else will have better luck debugging the parser, without docs I'm just not quite understanding the pattern enough.

Scott