package com.fasterxml.jackson.jr.ob;

import java.util.*;

import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.JSON.Feature;

public class ReadSimpleTest extends TestBase
{
    enum ABC { A, B, C; }
    
    public void testSimpleList() throws Exception
    {
        final String INPUT = "[1,2,3]";
        Object ob = JSON.std.anyFrom(INPUT);
        // default mapping should be to List:
        assertTrue(ob instanceof List);
        assertEquals(3, ((List<?>) ob).size());
        // actually, verify with write...
        assertEquals(INPUT, JSON.std.asString(ob));

        // but same should be possible with explicit call as well
        List<Object> list = JSON.std.listFrom(INPUT);
        assertEquals(3, list.size());
        assertEquals(INPUT, JSON.std.asString(list));
    }

    public void testSimpleArray() throws Exception
    {
        final String INPUT = "[true,\"abc\"]";

        // first: can explicitly request an array:
        Object ob = JSON.std.arrayFrom(INPUT);
        assertTrue(ob instanceof Object[]);
        assertEquals(2, ((Object[]) ob).length);
        assertEquals(INPUT, JSON.std.asString(ob));

        // or by changing default mapping:
        ob = JSON.std.with(Feature.READ_JSON_ARRAYS_AS_JAVA_ARRAYS).anyFrom(INPUT);
        assertTrue(ob instanceof Object[]);
        assertEquals(2, ((Object[]) ob).length);
        assertEquals(INPUT, JSON.std.asString(ob));
    }

    public void testSimpleMap() throws Exception
    {
        final String INPUT = "{\"a\":1,\"b\":true,\"c\":3}";
        Object ob = JSON.std.anyFrom(INPUT);
        assertTrue(ob instanceof Map);
        assertEquals(3, ((Map<?,?>) ob).size());
        // actually, verify with write...
        assertEquals(INPUT, JSON.std.asString(ob));
    }

    public void testSimpleMixed() throws Exception
    {
        final String INPUT = "{\"a\":[1,2,{\"b\":true},3],\"c\":3}";
        Object ob = JSON.std.anyFrom(INPUT);
        assertTrue(ob instanceof Map);
        assertEquals(2, ((Map<?,?>) ob).size());
        Object list = (((Map<?,?>) ob).get("a"));
        assertTrue(list instanceof List<?>);
        
        // actually, verify with write...
        assertEquals(INPUT, JSON.std.asString(ob));
    }

    public void testSimpleEnums() throws Exception
    {
        // First using index
        ABC abc = JSON.std.beanFrom(ABC.class, String.valueOf(ABC.B.ordinal()));
        assertEquals(ABC.B, abc);

        // then from name
        abc = JSON.std.beanFrom(ABC.class, quote("C"));
        assertEquals(ABC.C, abc);
    }
}

@@ -215,16 +215,404 @@
  B, C; %7D
+%0A%0A    static class WithEnumMap %7B%0A        private Map%3CABC, String%3E values;%0A%0A        WithEnumMap() %7B %7D%0A        public WithEnumMap(ABC key, String value) %7B%0A            values = new LinkedHashMap%3CABC,String%3E();%0A            values.put(key, value);%0A        %7D%0A%0A        public Map%3CABC, String%3E getValues() %7B return values; %7D%0A        public void setValues(Map%3CABC, String%3E v) %7B values = v; %7D%0A    %7D
 %0A    %0A  
@@ -2963,11 +2963,884 @@
 );%0A    %7D
+%0A%0A    // %5Bissue#21%5D%0A    public void testMapWithEnumKey() throws Exception%0A    %7B%0A        WithEnumMap input = new WithEnumMap(ABC.B, %22bar%22);%0A        // verify serialization, should be ok:%0A        String json = JSON.std.asString(input);%0A        assertEquals(aposToQuotes(%22%7B'values':%7B'B':'bar'%7D%7D%22), json);%0A%0A        // and then get it back too%0A        WithEnumMap result = JSON.std.beanFrom(WithEnumMap.class, json);%0A        assertNotNull(result);%0A        Map%3CABC, String%3E map = result.getValues();%0A        assertNotNull(map);%0A        assertEquals(1, map.size());%0A        Map.Entry%3C?,?%3E entry = map.entrySet().iterator().next();%0A        assertEquals(%22bar%22, entry.getValue());%0A        if (!(entry.getKey() instanceof ABC)) %7B%0A            fail(%22Expected key to be of type ABC, is: %22+entry.getKey().getClass().getName());%0A        %7D%0A        assertEquals(ABC.B, entry.getKey());%0A    %7D
 %0A%7D%0A

Add a test for #21