4

I've been making a custom library for converting an object array into a json string and also posting it. I've recently come into a problem where I cannot detect an internal array.

public static string ConvertArray( params object[] jsonData ) {
        if( jsonData.Length % 2 != 0 ) throw new ArgumentException( string.Format( "Key \'{0}\' missing value", jsonData[jsonData.Length - 1] ) );
        string json = "{";
        for( int i = 0; i < jsonData.Length; ) {
            json += string.Format( "\"{0}\":", jsonData[i++] );
            if( jsonData[i] is string ) {
                Console.WriteLine( "Found a string" );
                json += string.Format( "\"{0}\",", jsonData[i++] );
            } else if( jsonData[i].GetType( ).IsArray ) {
                Console.WriteLine( "Found an internal array" );
                json += string.Format( "{0},", ConvertArray( jsonData[i++] ) );
            } else {
                if( jsonData[i] is byte || jsonData[i] is sbyte || jsonData[i] is int || jsonData[i] is uint || jsonData[i] is short || jsonData[i] is ushort || jsonData[i] is long || jsonData[i] is ulong || jsonData[i] is float || jsonData[i] is double || jsonData[i] is bool || jsonData[i] is decimal ) {
                    Console.WriteLine( "Found a generic" );
                    json += string.Format( "{0},", jsonData[i++] );
                } else if( jsonData[i] is char ) {
                    Console.WriteLine( "Found a char" );
                    json += string.Format( "\'{0}\',", jsonData[i++] );
                } else {
                    Console.WriteLine( "Found an object" );
                    object work = jsonData[i++];
                    try {
                        MethodInfo workMethod = work.GetType().GetMethod("ToJsonString");
                        json += string.Format( "{0},", workMethod.Invoke( work, new object[] { } ) );
                    } catch {
                        try {
                            using( MemoryStream memStr = new MemoryStream( ) ) {
                                new BinaryFormatter( ).Serialize( memStr, work );
                                memStr.Seek( 0, SeekOrigin.Begin );
                                using( StreamReader strReader = new StreamReader( memStr ) ) {
                                    json += string.Format( "\"{0}\",", strReader.ReadToEnd( ) );
                                }
                            }
                        } catch {
                            throw new ArgumentException( "The value for key \'{0}\' does not contain a public method ToJsonString and cannot be serialized" );
                        }
                    }
                }
            }
        }
        return json + "}";
    }

I've been running it with:

string jsonString = JsonWebUtil.ConvertArray( new object[] {
            "jsonrpc","2.0",
            "method","generateIntegers",
            "params",JsonWebUtil.ConvertArray( new object[] {
                "apiKey", "<REDACTED>",
                "n", 14,
                "min", 0,
                "max", 10,
            } ),
            "id", 10461
        } );

And I get the following output:

{"jsonrpc":"2.0","method":"generateIntegers","params":"{"apiKey":"<REDACTED>","n":14,"min":0,"max":10,}","id":10461,}

There shouldn't be a quotation arround the internal array yet there is. And when it runs through my code the console shows that it is detected as what I declared as generics. I have no idea why jsonData[i].getType().IsArray isn't returning the correct value.

4
  • Have you run it in the debugger? What does jsonData[i].getType() show you there? Commented Jan 1, 2016 at 3:33
  • Also why are you incrementing i within the loop instead of in the loop definition? Commented Jan 1, 2016 at 3:35
  • 2
    any reason you are re-inventing the wheel. there are a lot of decent libraries out there already...newtonsoft.com/json Commented Jan 1, 2016 at 3:35
  • The same reason anything is reinvented, because I wanted to do it myself and I wanted the satisfaction of accomplishing it. Commented Jan 1, 2016 at 3:51

1 Answer 1

4

None of your parameters are arrays - they are all strings or numbers. The parameter you may think is an array is not. Since you are wrapping it in a call to JsonWebUtil.ConvertArray is it getting converted to a string.

I supect you want something like:

string jsonString = JsonWebUtil.ConvertArray( new object[] {
        "jsonrpc","2.0",
        "method","generateIntegers",
        "params", new object[] {
            "apiKey", "<REDACTED>",
            "n", 14,
            "min", 0,
            "max", 10,
            } ,
        "id", 10461
    } );

Other things that may trip you up:

  • strings that contain quotes
  • catching an exception to determine that no ToJsonString method exists - why not just check if workMethod is null?
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I kept reading this wrong. Also, internal quotations should never be a problem as they would never be valid in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.