Jump to content

Search the Community

Showing results for tags 'json'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • RuneLocus
    • News & Announcements
    • Information Booth
    • Website Support
    • Introductions
    • Chat
    • Forum Games
  • RuneScape Development
    • RSPS General
    • RuneScape Private Server (RS2)
    • RuneScape Private Server (503+)
    • RuneScape Private Server (EoC 742+)
  • Other
    • Digital Art
    • Gaming
    • Webdevelopment
    • Computers
  • Marketplace
    • RuneScape Market
    • RSPS Market
    • Others Market
    • Freelance Middleman Services
  • Super Secret Club's Topics

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 4 results

  1. Since a lot of people are beginning to use Asteria (based of runesource and heavily edited by lare96) I thought it would be helpful for members who wish to use that base and would like some NPC Spawns. Link: here Preview: { "npc-id": 1160, "position": { "x": 3479, "y": 9492, "z": 0 }, "walking-policy": { "coordinate": false, "radius": 0 } }[/CODE]
  2. I was wondering what the benefits of using JSON for things like npcspawns is more beneficial than the default cfg.
  3. JSONEncoder [CODE]package com.argon.json; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.beans.IntrospectionException; import java.io.Writer; import java.io.IOException; import java.io.StringWriter; import java.lang.reflect.Array; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.Date; import java.util.List; import java.util.Iterator; import java.util.Map.Entry; import java.util.ArrayList; import java.util.Collections; import java.util.WeakHashMap; public class JSONEncoder<T> { private static final Map<Class<?>, ObjectEncoder> encoderCache = Collections.synchronizedMap(new WeakHashMap<Class<?>, ObjectEncoder>()); private final Encoder encoder; private JSONEncoder(final Class<T> type) throws IntrospectionException { encoder = getEncoder(type); } [MENTION=19271]supp[/MENTION]ressWarnings("unchecked") private static Encoder getEncoder(final Class<?> type) throws IntrospectionException { if(CharSequence.class.isAssignableFrom(type) || Character.class.equals(type) || Character.TYPE.equals(type) || UUID.class.equals(type)) { return StringEncoder.INSTANCE; } else if(Number.class.isAssignableFrom(type) || Byte.TYPE.equals(type) || Short.TYPE.equals(type) || Integer.TYPE.equals(type) || Long.TYPE.equals(type) || Boolean.TYPE.equals(type) || Boolean.class.equals(type)) { return PlainFormEncoder.INSTANCE; } else if(type.isArray()) { return new ArrayEncoder(getEncoder(type.getComponentType())); } else if(Iterable.class.isAssignableFrom(type)) { return IterableEncoder.INSTANCE; } else if(Map.class.isAssignableFrom(type)) { return MapEncoder.INSTANCE; } else if(Date.class.isAssignableFrom(type)) { return DateEncoder.INSTANCE; } else { ObjectEncoder encoder = encoderCache.get(type); if(encoder == null) { encoder = new ObjectEncoder(type); encoderCache.put(type, encoder); } return encoder; } } public String encode(final T value) { final StringWriter out = new StringWriter(); try { encode(value, out); } catch(IOException ex) { // ignore this... it won't happen with a StringWriter } return out.toString(); } public void encode(final T value, final Writer out) throws IOException { encoder.encode(value, out); } public static <T> JSONEncoder<T> getJSONEncoder(final Class<T> type) throws IntrospectionException { return new JSONEncoder<T>(type); } private static class Property { private final String name; private final Method getter; private final Encoder encoder; public Property(final String name, final Method getter) throws IntrospectionException { this.name = name; this.getter = getter; this.encoder = getEncoder(getter.getReturnType()); } private void encode(final Object object, final Writer out) throws IOException { try { out.write(name); out.write(':'); final Object value = getter.invoke(object); if(value == null) { out.write("null"); } else { encoder.encode(value, out); } } catch(IllegalAccessException ex) { throw new IOException(ex); } catch(IllegalArgumentException ex) { throw new IOException(ex); } catch(InvocationTargetException ex) { throw new IOException(ex); } } } private static interface Encoder { void encode(Object value, Writer out) throws IOException; } private static class PlainFormEncoder implements Encoder { private static final PlainFormEncoder INSTANCE = new PlainFormEncoder(); public PlainFormEncoder() { } public void encode(final Object value, final Writer out) throws IOException { out.write(value.toString()); } } private static class DateEncoder implements Encoder { private static final DateEncoder INSTANCE = new DateEncoder(); public DateEncoder() { } public void encode(final Object value, final Writer out) throws IOException { out.write("new Date("); out.write(Long.toString(((Date)value).getTime())); out.write(")"); } } private static class StringEncoder implements Encoder { private static final StringEncoder INSTANCE = new StringEncoder(); public StringEncoder() { } public void encode(final Object value, final Writer out) throws IOException { out.write('\"'); final String string = value.toString(); for(int i = 0; i < string.length(); i++) { final char ch = string.charAt(i); switch(ch) { case '\n': out.write("\\n"); break; case '\r': out.write("\\r"); break; case '\"': out.write("\\\""); break; default: out.write(ch); break; } } out.write('\"'); } } private static class ArrayEncoder implements Encoder { private final Encoder encoder; public ArrayEncoder(final Encoder encoder) { this.encoder = encoder; } public void encode(final Object value, final Writer out) throws IOException { out.write('['); final int length = Array.getLength(value); for(int i = 0; i < length; i++) { encoder.encode(Array.get(value, i), out); if(i + 1 < length) { out.write(','); } } out.write(']'); } } private static class IterableEncoder implements Encoder { private static final IterableEncoder INSTANCE = new IterableEncoder(); public IterableEncoder() { } public void encode(final Object value, final Writer out) throws IOException { final Iterator<? extends Object> iterator = ((Iterable<?>)value).iterator(); Encoder lastEncoder = null; Class<?> lastType = null; out.write('['); while(iterator.hasNext()) { try { final Object instance = iterator.next(); final Class<?> type = instance.getClass(); final Encoder enc = type == lastType ? lastEncoder : getEncoder(type); enc.encode(instance, out); lastType = type; lastEncoder = enc; } catch(IntrospectionException ie) { throw new IOException(ie); } if(iterator.hasNext()) { out.write(','); } } out.write(']'); } } private static class MapEncoder implements Encoder { private static final MapEncoder INSTANCE = new MapEncoder(); public MapEncoder() { } public void encode(final Object value, final Writer out) throws IOException { [MENTION=19271]supp[/MENTION]ressWarnings("unchecked") final Map<Object, Object> map = (Map<Object, Object>)value; final Set<Map.Entry<Object, Object>> entrySet = map.entrySet(); final Iterator<Map.Entry<Object, Object>> iterator = entrySet.iterator(); Encoder lastEncoder = null; Class<?> lastType = null; out.write('{'); while(iterator.hasNext()) { try { final Map.Entry<Object, Object> entry = iterator.next(); final Object entryValue = entry.getValue(); final Class<?> type = entryValue.getClass(); final Encoder enc = type == lastType ? lastEncoder : getEncoder(type); out.write(entry.getKey().toString()); out.write(':'); enc.encode(entryValue, out); lastType = type; lastEncoder = enc; } catch(IntrospectionException ie) { throw new IOException(ie); } if(iterator.hasNext()) { out.write(','); } } out.write('}'); } } private static class ObjectEncoder implements Encoder { private final Property[] properties; public ObjectEncoder(final Class<?> type) throws IntrospectionException { final BeanInfo info = Introspector.getBeanInfo(type); final PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); final List<Property> props = new ArrayList<Property>(descriptors.length); for(int i = 0; i < descriptors.length; i++) { final PropertyDescriptor d = descriptors[i]; final Method read = d.getReadMethod(); if(!read.getDeclaringClass().equals(Object.class)) { props.add(new Property(d.getName(), read)); } } properties = props.toArray(new Property[props.size()]); } public void encode(final Object value, final Writer out) throws IOException { out.write('{'); final int length = properties.length; for(int i = 0; i < length; i++) { properties[i].encode(value, out); if(i + 1 < length) { out.write(','); } } out.write('}'); } } }[/CODE] And the JSONDecoder [CODE]package com.argon.json; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.beans.IntrospectionException; import java.io.Reader; import java.io.IOException; import java.io.StringReader; import java.io.StreamTokenizer; import java.lang.reflect.Type; import java.lang.reflect.Array; import java.lang.reflect.Modifier; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.ParameterizedType; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Map; import java.util.Set; import java.util.Date; import java.util.List; import java.util.Queue; import java.util.HashMap; import java.util.HashSet; import java.util.TreeSet; import java.util.SortedSet; import java.util.ArrayList; import java.util.LinkedList; import java.util.Collection; import java.util.Collections; import java.util.WeakHashMap; import java.util.NavigableSet; public class JSONDecoder { private static final DateFormat[] DATE_FORMATS = new DateFormat[]{ new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyyMMdd"), new SimpleDateFormat("yyyyMMdd'T'HHmmss") }; private static final Map<Class<?>, Map<String, PropertyDescriptor>> PROPERTY_CACHE = Collections.synchronizedMap(new WeakHashMap<Class<?>, Map<String, PropertyDescriptor>>()); public static <T> T decode(final String json, final Class<T> type) { try { return decode(new StringReader(json), type); } catch(IOException ex) { throw new RuntimeException(ex); } } public static <T> T decode(final Reader in, final Class<T> type) throws IOException { final ObjectBuilder builder = createObjectBuilder(type); final JSONScanner scanner = new JSONScanner(in); scanner.consumeObject(builder); [MENTION=19271]supp[/MENTION]ressWarnings("unchecked") final T value = (T)builder.createObject(); return value; } private static Map<String, PropertyDescriptor> createClassPropertyMap(final Class<?> type) throws IntrospectionException { final BeanInfo info = Introspector.getBeanInfo(type); final PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); final int len = descriptors.length; final Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>(len); for(final PropertyDescriptor desc : descriptors) { if(desc.getWriteMethod() != null) { properties.put(desc.getName(), desc); } } return properties; } private static Map<String, PropertyDescriptor> getClassProperties(final Class<?> type) throws IntrospectionException { Map<String, PropertyDescriptor> properties = PROPERTY_CACHE.get(type); synchronized(PROPERTY_CACHE) { if(properties == null) { properties = createClassPropertyMap(type); PROPERTY_CACHE.put(type, properties); } } return properties; } private static void testParameterizedType(final ParameterizedType type, final Class<?> expectedRawType, final Class<?>... parameters) throws IOException { final Type rawType = type.getRawType(); if(rawType instanceof Class) { final Class<?> rawClass = (Class<?>)rawType; if(!expectedRawType.isAssignableFrom(rawClass)) { throw new IOException("Expected a raw type of " + expectedRawType.getName() + " but got " + rawClass.getName()); } } else { throw new IOException("Expected a class as the raw type of " + type); } final Type[] paramTypes = type.getActualTypeArguments(); if(paramTypes.length != parameters.length) { throw new IOException("Incorrect number of type parameters, expected " + parameters.length + " but found " + paramTypes.length + ": " + type); } for(int i = 0; i < parameters.length; i++) { if(paramTypes[i] instanceof Class) { final Class<?> paramClass = (Class<?>)paramTypes[i]; final Class<?> expected = parameters[i]; if(expected != null && !expected.isAssignableFrom(paramClass)) { throw new IOException("Exptected a parameter of type " + expected.getName() + " but got " + paramClass.getName()); } } else { throw new IOException("Expected a class parameter, but got " + paramTypes[i]); } } } private static ObjectBuilder createObjectBuilder(final Type type) throws IOException { if(type == null) { return new NullObjectBuilder(); } else if(type instanceof Class) { try { return new DefaultObjectBuilder((Class<?>)type); } catch(InstantiationException ex) { throw new IOException(ex); } catch(IllegalAccessException ex) { throw new IOException(ex); } catch(IntrospectionException ex) { throw new IOException(ex); } } else if(type instanceof ParameterizedType) { final ParameterizedType paramType = (ParameterizedType)type; testParameterizedType(paramType, Map.class, String.class, null); return new MapObjectBuilder(paramType.getActualTypeArguments()[1]); } else { throw new IOException("Cannot handle objects of type " + type); } } private static Object convertNumber(final double num, final Class<?> type) throws IOException { if(type == byte.class || type == Byte.class) { return Byte.valueOf((byte)num); } else if(type == short.class || type == Short.class) { return Short.valueOf((short)num); } else if(type == int.class || type == Integer.class) { return Integer.valueOf((int)num); } else if(type == long.class || type == Long.class) { return Long.valueOf((long)num); } else if(type == float.class || type == Float.class) { return Float.valueOf((float)num); } else if(type == double.class || type == Double.class) { return Double.valueOf(num); } else { return null; } } private static Object convertString(final String value, final Class<?> type) throws IOException { if(type == String.class || CharSequence.class.isAssignableFrom(type)) { return value; } else if(type == char.class || type == Character.class) { if(value.length() == 0) { return null; } else { return value.charAt(0); } } else { return null; } } private static interface ObjectBuilder { Type getFieldType(final String fieldName); void set(final String fieldName, final Object builder) throws IOException; Object createObject(); } private static class NullObjectBuilder implements ObjectBuilder { public NullObjectBuilder() { } public Type getFieldType(final String fieldName) { return null; } public void set(final String fieldName, final Object builder) { } public Object createObject() { return null; } } private static class DefaultObjectBuilder implements ObjectBuilder { private final Map<String, PropertyDescriptor> properties; private final Object object; DefaultObjectBuilder(Class<?> type) throws InstantiationException, IllegalAccessException, IntrospectionException { properties = getClassProperties(type); object = type.newInstance(); } public Type getFieldType(final String fieldName) { final PropertyDescriptor desc = properties.get(fieldName); return desc != null ? desc.getWriteMethod().getGenericParameterTypes()[0] : null; } public void set(final String fieldName, final Object value) throws IOException { final PropertyDescriptor desc = properties.get(fieldName); if(desc != null && value != null) { try { desc.getWriteMethod().invoke(object, value); } catch(IllegalAccessException iae) { throw new IOException(iae); } catch(IllegalArgumentException iae) { throw new IOException(iae); } catch(InvocationTargetException ite) { throw new IOException(ite); } } } public Object createObject() { return object; } } private static class MapObjectBuilder implements ObjectBuilder { private final Type valueType; private final Map<String, Object> map = new HashMap<String, Object>(); public MapObjectBuilder(Type valueType) { this.valueType = valueType; } public Type getFieldType(final String fieldName) { return valueType; } public void set(final String fieldName, final Object value) throws IOException { map.put(fieldName, value); } public Object createObject() { return map; } } private static class JSONScanner { private final StreamTokenizer in; JSONScanner(final Reader in) { this.in = new StreamTokenizer(in); configureTokenizer(); } private void configureTokenizer() { in.quoteChar('\''); in.quoteChar('\"'); in.eolIsSignificant(false); in.ordinaryChar(':'); in.ordinaryChar('['); in.ordinaryChar(']'); in.ordinaryChar('{'); in.ordinaryChar('}'); in.ordinaryChar(','); } [MENTION=19271]supp[/MENTION]ressWarnings("unchecked") private <T> void consumeJSONArray(final Class<T> valueType, final Collection<T> objects) throws IOException { boolean done = false; while(!done) { if(in.nextToken() == ']') { done = true; } else { in.pushBack(); objects.add((T)consumeValue(valueType)); } if(in.nextToken() != ',') { in.pushBack(); } } } private <T> Object consumeJavaArray(final Class<T> valueType) throws IOException { final List<T> objects = new ArrayList<T>(); consumeJSONArray(valueType, objects); return toArrayHelper(objects, valueType); } [MENTION=19271]supp[/MENTION]ressWarnings("unchecked") private <T> Object consumeJavaCollection(final Type arrayType) throws IOException { final ParameterizedType paramType = (ParameterizedType)arrayType; testParameterizedType(paramType, Collection.class, Object.class); final Class<?> rawClass = (Class<?>)paramType.getRawType(); final Class<T> valueType = (Class<T>)paramType.getActualTypeArguments()[0]; Collection<T> output = null; if(rawClass.isInterface()) { if(rawClass.equals(List.class) || rawClass.equals(Collection.class)) { output = new ArrayList<T>(); } else if(rawClass.equals(Set.class)) { output = new HashSet<T>(); } else if(rawClass.equals(SortedSet.class) || rawClass.equals(NavigableSet.class)) { output = new TreeSet<T>(); } else if(rawClass.equals(Queue.class)) { output = new LinkedList<T>(); } else { throw new IOException("Unknown Collection type: " + rawClass.getName()); } } else if(Modifier.isAbstract(rawClass.getModifiers())) { throw new IOException("Cannot work with an abstract Collection: " + rawClass); } else { try { output = (Collection<T>)rawClass.newInstance(); } catch(InstantiationException ie) { throw new IOException(ie); } catch(IllegalAccessException iae) { throw new IOException(iae); } } consumeJSONArray(valueType, output); return output; } [MENTION=19271]supp[/MENTION]ressWarnings("unchecked") private <T> T[] toArrayHelper(final List<T> values, final Class<T> componentType) { return values.toArray((T[])Array.newInstance(componentType, values.size())); } void consume(final int expected) throws IOException { if(in.nextToken() != expected) { throw new IOException("Expected " + (char)expected + " but instead found " + (char)in.ttype); } } void consume(final String expected) throws IOException { if(in.nextToken() != StreamTokenizer.TT_WORD) { throw new IOException("Expected a word."); } if(!in.sval.equals(expected)) { throw new IOException("Expected '" + expected + "' but found '" + in.sval + "' instead"); } } Object consumeArray(final Type arrayType) throws IOException { if(arrayType == null) { return null; } if(arrayType instanceof Class) { final Class<?> type = (Class<?>)arrayType; if(!type.isArray()) { throw new IOException("Expected an array type, but got: " + type.toString()); } return consumeJavaArray(type.getComponentType()); } else if(arrayType instanceof ParameterizedType) { return consumeJavaCollection(arrayType); } else { throw new IOException("Expected either an array type or ParameterizedType"); } } Date consumeDate() throws IOException, IOException { consume("Date"); consume('('); Date value = null; switch(in.nextToken()) { case StreamTokenizer.TT_NUMBER: value = new Date((long)in.nval); break; case '\'': case '\"': for(final DateFormat fmt : DATE_FORMATS) { synchronized(fmt) { try { value = fmt.parse(in.sval); break; } catch(ParseException ex) { // ignore this } } } break; default: throw new IOException("Expected an ISO date / datetime string, or " + "a millisecond count."); } if(in.nextToken() != StreamTokenizer.TT_NUMBER) { throw new IOException("Expected a word."); } consume(')'); return value; } Object consumeValue(final Type genericType) throws IOException { switch(in.nextToken()) { case '{': final ObjectBuilder fieldObject = createObjectBuilder(genericType); in.pushBack(); consumeObject(fieldObject); return fieldObject.createObject(); case '\"': case '\'': return convertString(in.sval, (Class<?>)genericType); case '[': return consumeArray(genericType); case StreamTokenizer.TT_NUMBER: return convertNumber(in.nval, (Class<?>)genericType); case StreamTokenizer.TT_WORD: if(in.sval.equals("true") || in.sval.equals("false")) { return Boolean.valueOf(in.sval); } else if(in.sval.equals("new")) { return consumeDate(); } else if(in.sval.equals("null")) { return null; } else { throw new IOException("Expected 'true', 'false' or 'new', but got '" + in.sval + "'"); } } return null; } boolean consumeField(final ObjectBuilder into) throws IOException { consume(StreamTokenizer.TT_WORD); final String identifier = in.sval; consume(':'); into.set(identifier, consumeValue(into.getFieldType(identifier))); switch(in.nextToken()) { case ',': return true; case '}': in.pushBack(); return false; default: throw new IOException("Expected one of ',' or '}' but got '" + (char)in.ttype + "'"); } } void consumeObject(final ObjectBuilder into) throws IOException { consume('{'); while(consumeField(into)) { ; } consume('}'); } } }[/CODE]
  4. Arix

    JSON Server

    Hey guys, I was bored and I decided to give runelocus a little tool that is in my opinion potentially quite useful. [B][COLOR="#FF0000"]It works for any revision![/COLOR][/B] For those who don't know what JSON is: [URL="http://json.org/"]What is JSON[/URL] Basically, this tool provides a very simple server to output JSON in real time from the server to a browser (because it sends HTTP headers). This could be useful if you wanted for example the number of players online, the uptime or more complicated stuff. I made it as simple as I possibly could think of at the time. All you have to do is: [code] JSON.get().start(port); [/code] where port is a port of your choosing. You would have to portforward this port too, ofcourse. Then, anywhere you want to fill in data you may use this line: [code] JSON.get().data().put("testvalue", 1000); [/code] the JSON.get().data() method returns a JSONObject made by the json.org foundation. Feel free to check out the methods inside the JSONObject but for this tool you will only need the two lines of code. Example output (after visiting [url]http://myserveriporurl:myport[/url])[COLOR="#FF0000"]*[/COLOR]: [code] {"testvalue":1000} [/code] [COLOR="#FF0000"]*[/COLOR] [COLOR="#FF8C00"]When you put the port on 80 you will not have to add your port to the end of the server url.[/COLOR] [B]Note: [/B]I am assuming you have decent knowledge and know how to import this library into your classpath and know where to use it. Here is the download: [URL="https://mega.co.nz/#!ktgy1I6B!8KyrrDgwIcn7icGpOvUrhAgvx6Lj8HOX-lEJ0I2ZciA"]RS-JSON.jar[/URL] [B]EDIT: [/B] This tool works fine however I did not take proper planning and the best care for the code. It was more like.. "what to do?". Having told you that, hope you'll put it to good use :) [B]EDIT 2: [/B] This does not have to be used for RSPS. It can be used in any java project where you may find it useful. [B]EDIT 3: [/B] Showing how to access the data. [B]EDIT 4: [/B] Tutorial on how to use it! [url]http://www.runelocus.com/forums/showthread.php?100807-JSON-Server&p=784913&viewfull=1#post784913[/url]
×